How to print using XDocument

By using XDeclaration. This will add the declaration. But with ToString() you will not get the desired output. You need to use XDocument.Save() with one of his methods. Full sample: var doc = new XDocument( new XDeclaration(“1.0”, “utf-16”, “yes”), new XElement(“blah”, “blih”)); var wr = new StringWriter(); doc.Save(wr); Console.Write(wr.ToString());

How can I write xml with a namespace and prefix with XElement?

Here is a small example that creates the output you want: using System; using System.Xml.Linq; class Program { static void Main() { XNamespace ci = “http://somewhere.com”; XNamespace ca = “http://somewhereelse.com”; XElement element = new XElement(“root”, new XAttribute(XNamespace.Xmlns + “ci”, ci), new XAttribute(XNamespace.Xmlns + “ca”, ca), new XElement(ci + “field1”, “test”), new XElement(ca + “field2”, “another … Read more

Parsing XML using XDocument

I suspect you’re being stumped by the namespace. Try this: XDocument doc = XDocument.Load(“test.xml”); XNamespace ns = “http://ns.adobe.com/xfdf/”; foreach (XElement element in doc.Root .Element(ns + “fields”) .Elements(ns + “field”)) { Console.WriteLine(“Name: {0}; Value: {1}”, (string) element.Attribute(“name”), (string) element.Element(ns + “value”)); } Or to find just the one specific element: XDocument doc = XDocument.Load(“test.xml”); XNamespace ns … Read more

C# XDocument Load with multiple roots

XmlReader itself does support reading of xml fragment – i.e. var settings = new XmlReaderSettings { ConformanceLevel = ConformanceLevel.Fragment }; using (var reader = XmlReader.Create(“fragment.xml”, settings)) { // you can work with reader just fine } However XDocument.Load does not support reading of fragmented xml. Quick and dirty way is to wrap the nodes under … Read more

XmlTextReader vs. XDocument

If you’re happy reading everything into memory, use XDocument. It’ll make your life much easier. LINQ to XML is a lovely API. Use an XmlReader (such as XmlTextReader) if you need to handle huge XML files in a streaming fashion, basically. It’s a much more painful API, but it allows streaming (i.e. only dealing with … Read more

XML file creation using XDocument in C#

LINQ to XML allows this to be much simpler, through three features: You can construct an object without knowing the document it’s part of You can construct an object and provide the children as arguments If an argument is iterable, it will be iterated over So here you can just do: void Main() { List<string> … Read more

How to Get XML Node from XDocument

test.xml: <?xml version=”1.0″ encoding=”utf-8″?> <Contacts> <Node> <ID>123</ID> <Name>ABC</Name> </Node> <Node> <ID>124</ID> <Name>DEF</Name> </Node> </Contacts> Select a single node: XDocument XMLDoc = XDocument.Load(“test.xml”); string id = “123”; // id to be selected XElement Contact = (from xml2 in XMLDoc.Descendants(“Node”) where xml2.Element(“ID”).Value == id select xml2).FirstOrDefault(); Console.WriteLine(Contact.ToString()); Delete a single node: XDocument XMLDoc = XDocument.Load(“test.xml”); string id … Read more