get xelement attribute value

var xml = @”<User ID=””11″” Name=””Juan Diaz”” LoginName=””DN1\jdiaz”” xmlns=””http://schemas.microsoft.com/sharepoint/soap/directory/”” />”; var user = XElement.Parse(xml); var login = user.Attribute(“LoginName”).Value; // “DN1\jdiaz”

how to add XElement in specific location in XML Document

Search element you want to add and use Add method as shown below xDoc.Element(“content”) .Elements(“item”) .Where(item => item.Attribute(“id”).Value == “2”).FirstOrDefault() .AddAfterSelf(new XElement(“item”, “C”, new XAttribute(“id”, “3”))); or <Microsoft> <DOTNet> </DOTNet> </Microsoft> private void addToXml() { XDocument xmlDoc = XDocument.Load(“yourfile.xml”); xmlDoc.Element(“Microsoft”).Add(new XElement(“DOTNet”, new XElement(“Name”, “Nisar”), new XElement(“Forum”, “dotnetobject”), new XElement(“Position”, “Member”))); xmlDoc.Save(“yourfile.xml”); readXml(); } <Microsoft> <DOTNet> … Read more

How can I remove empty xmlns attribute from node created by XElement

It’s all about how you handle your namespaces. The code below creates child items with different namespaces: XNamespace defaultNs = “http://www.tempuri.org/default”; XNamespace otherNs = “http://www.tempuri.org/other”; var root = new XElement(defaultNs + “root”); root.Add(new XAttribute(XNamespace.Xmlns + “otherNs”, otherNs)); var parent = new XElement(otherNs + “parent”); root.Add(parent); var child1 = new XElement(otherNs + “child1”); parent.Add(child1); var child2 … Read more

Convert XDocument to Stream

In .NET 4 and later, you can Save it to a MemoryStream: Stream stream = new MemoryStream(); doc.Save(stream); // Rewind the stream ready to read from it elsewhere stream.Position = 0; In .NET 3.5 and earlier, you would need to create an XmlWriter based on a MemoryStream and save to that, as shown in dtb’s … Read more

Understanding Linq To Xml – Descendants return no results

var result = doc.Descendants(“TransactionInformationType”); selects all descendants in the XDocument that have element name “TransactionInformationType” and are in the empty namespace. From you screenshot it seems the element you’re trying to select is in the namespace “https://ssl.ditonlinebetalingssystem.dk/remote/payment” though. You need to specify that explicitly: XNamespace ns = “https://ssl.ditonlinebetalingssystem.dk/remote/payment”; ↑↑ ↑ var result = doc.Descendants(ns + … Read more

Force XDocument to write to String with UTF-8 encoding

Try this: using System; using System.IO; using System.Text; using System.Xml.Linq; class Test { static void Main() { XDocument doc = XDocument.Load(“test.xml”, LoadOptions.PreserveWhitespace); doc.Declaration = new XDeclaration(“1.0”, “utf-8”, null); StringWriter writer = new Utf8StringWriter(); doc.Save(writer, SaveOptions.None); Console.WriteLine(writer); } private class Utf8StringWriter : StringWriter { public override Encoding Encoding { get { return Encoding.UTF8; } } } … Read more

Convert XElement to string

ToString should most definitely work. I use it all the time. What does it return for you in this case? An empty string? My guess is that something went wrong building your XElement. To debug, rewrite the code to add each of the child XElements separately, so that you can step through your code and … Read more

How do I get a NameTable from an XDocument?

You need to shove the XML through an XmlReader and use the XmlReader’s NameTable property. If you already have Xml you are loading into an XDocument then make sure you use an XmlReader to load the XDocument:- XmlReader reader = new XmlTextReader(someStream); XDocument doc = XDocument.Load(reader); XmlNameTable table = reader.NameTable; If you are building Xml … Read more