using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; using System.IO; public class MainClass{ public static void Main(string[] args){ XDocument NewDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("Root", "MyDoc")); // Display it on screen. Console.WriteLine(NewDoc.Declaration.ToString() + " " + NewDoc.Document.ToString()); } }
Define the namespace
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; using System.IO; public class MainClass{ public static void Main(string[] args){ XDocument NewDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("Root", "MyDoc")); XNamespace MyNS = "http://www.kutayzorlu.com/java2s/com/"; NewDoc.Element("Root").Name = MyNS.GetName("Root"); // Add an element that uses the namespace. NewDoc.Element(MyNS + "Root").Add( new XElement(MyNS + "Child1", "Some Data1 "), new XElement(MyNS + "Child2", new XAttribute( XNamespace.Xmlns + "NewNS", "http://www.kutayzorlu.com/java2s/com"), "Some Data 2")); } }
Add an element that uses the namespace
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; using System.IO; public class MainClass{ public static void Main(string[] args){ XDocument NewDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("Root", "MyDoc")); XNamespace MyNS = "http://www.kutayzorlu.com/java2s/com/"; // Add the namespace to the root node. NewDoc.Element("Root").Name = MyNS.GetName("Root"); // Add an element that uses the namespace. NewDoc.Element(MyNS + "Root").Add( new XElement(MyNS + "Child1", "Some Data1 "), new XElement(MyNS + "Child2", new XAttribute(XNamespace.Xmlns + "NewNS", "http://www.kutayzorlu.com/java2s/com"),"Some Data 2")); } }
Add the processing instruction
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; using System.IO; public class MainClass{ public static void Main(string[] args){ XDocument NewDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("Root", "MyDoc")); // Add the processing instruction. NewDoc.AddFirst(new XProcessingInstruction( "xml-stylesheet", "type='text/xsl' href='MyXSL.XSL'")); } }
Add an attribute to the root node
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; using System.IO; public class MainClass{ public static void Main(string[] args){ XDocument NewDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("Root", "MyDoc")); // Add an attribute to the root node. NewDoc.Element("Root").Add(new XAttribute("MyAttr", "Attribute Value")); } }
Add a comment to XDocument
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; using System.IO; public class MainClass{ public static void Main(string[] args){ XDocument NewDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("Root", "MyDoc")); // Add a comment. NewDoc.AddFirst(new XComment("This is a comment.")); // Add another comment. NewDoc.Element("Root").Add(new XComment("This is another comment.")); // Add a third comment. NewDoc.Add(new XComment("This is the third comment.")); // Add some text. NewDoc.Element("Root").Add(new XText("This is some text.")); } }
Add the CData to XDocument
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Xml; using System.Xml.Linq; using System.IO; public class MainClass{ public static void Main(string[] args){ XDocument NewDoc = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XElement("Root", "MyDoc")); // Add the CData. NewDoc.Element("Root").Add( new XElement("CDataElement", new XCData("This is a CData Entry!"))); } }