using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "A"), new XElement("LastName", "B")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "C"), new XElement("LastName", "D")))); Console.WriteLine("Here is the original XML document:"); Console.WriteLine("{0}{1}{1}", xDocument, System.Environment.NewLine); } }
XML LINQ
XDocument transformation demo
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Xsl; using System.IO; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "A"), new XElement("LastName", "B")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "C"), new XElement("LastName", "D")))); XDocument transformedDoc = new XDocument(); using (XmlWriter writer = transformedDoc.CreateWriter()) { XslCompiledTransform transform = new XslCompiledTransform(); transform.Load(XmlReader.Create(new StringReader("c:your.xls"))); transform.Transform(xDocument.CreateReader(), writer); } Console.WriteLine(transformedDoc); } }
Use XDeclaration,XElement, XAttribute to create an XDocument
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data.Linq; using System.Reflection; using System.Xml.Linq; class Program { static void Main(string[] args) { XDocument xml = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement("people", new XElement("idperson", new XAttribute("id", 1), new XAttribute("year", 2004), new XAttribute("salary", "100")))); System.IO.StringWriter sw = new System.IO.StringWriter(); xml.Save(sw); Console.WriteLine(sw); } }
Creating a Declaration and Setting the Document's Declaration Property to It
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument(new XElement("BookParticipant")); XDeclaration xDeclaration = new XDeclaration("1.0", "UTF-8", "yes"); xDocument.Declaration = xDeclaration; Console.WriteLine(xDocument); } }
Creating a Declaration
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement("BookParticipant")); Console.WriteLine(xDocument); } }
Creating a Comment with Functional Construction
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XElement t = new XElement("Employee",new XComment("This person is retired.")); Console.WriteLine(t); } }
Calling the Second Remove Prototype
using System; using System.Linq; using System.Collections; using System.Collections.Generic; using System.Xml; using System.Xml.Linq; public class MainClass { public static void Main() { XDocument xDocument = new XDocument( new XElement("Books", new XElement("Book", new XAttribute("type", "Author"), new XComment("This is a new author."), new XElement("FirstName", "A"), new XElement("LastName", "B")), new XElement("Book", new XAttribute("type", "Author"), new XElement("FirstName", "C"), new XElement("LastName", "D")))); IEnumerable<XComment> comments = xDocument.Element("Books").Elements("Book").Nodes().OfType<XComment>(); foreach (XComment comment in comments) { Console.WriteLine("Source comment: {0}", comment); } comments.Remove(); Console.WriteLine(xDocument); } }