XElement
using System; using System.Collections.Generic; using System.Linq; using System.Xml.Linq; using System.IO; namespace LinqToXml { class Program { static void Main(string[] args) { #region Create //XElement contacts = // new XElement("Contacts", // new XElement("Contact", // new XElement("Name", "Patrick Hines"), // new XElement("Phone", "206-555-0144", // new XAttribute("Type", "Home")), // new XElement("phone", "425-555-0145", // new XAttribute("Type", "Work")), // new XElement("Address", // new XElement("Street1", "123 Main St"), // new XElement("City", "Mercer Island"), // new XElement("State", "WA"), // new XElement("Postal", "68042") // ) // ) //); //Console.WriteLine(contacts); #endregion #region Load&Select Stream stream = new FileStream(@"D:\MyCode\CSharpeDotNet\LinqToXml\LinqToXml\PurchaseOrder.xml", FileMode.Open); XElement xml = XElement.Load(stream); Console.WriteLine(xml); IEnumerable<string> partNos = from item in xml.Descendants("Item") select (string)item.Attribute("PartNumber"); IEnumerator<string> enumerator = partNos.GetEnumerator(); while (enumerator.MoveNext()) { Console.WriteLine(enumerator.Current); } #endregion #region Add //XElement srcTree = new XElement("Root", // new XElement("Element1", 1), // new XElement("Element2", 2), // new XElement("Element3", 3), // new XElement("Element4", 4), // new XElement("Element5", 5) //); //XElement xmlTree = new XElement("Root", // new XElement("NewElement", "Content") //); //xmlTree.Add( // from el in srcTree.Elements() // where (int)el >= 3 // select el //); //Console.WriteLine(xmlTree); #endregion #region Update //XElement root = new XElement("Root", // new XElement("Child", "child content") //); //Console.WriteLine(root); //root.SetValue("new content"); //Console.WriteLine(root); #endregion #region Remove //XElement xmlTree = new XElement("Root", // new XElement("Child1", "child1 content"), // new XElement("Child2", "child2 content"), // new XElement("Child3", "child3 content"), // new XElement("Child4", "child4 content"), // new XElement("Child5", "child5 content") // ); //Console.WriteLine(xmlTree); //XElement child3 = xmlTree.Element("Child3"); //child3.Remove(); //Console.WriteLine(xmlTree); #endregion #region ReplaceWith //XElement xmlTree = new XElement("Root", // new XElement("Child1", "child1 content"), // new XElement("Child2", "child2 content"), // new XElement("Child3", "child3 content"), // new XElement("Child4", "child4 content"), // new XElement("Child5", "child5 content") // ); //XElement child3 = xmlTree.Element("Child3"); //Console.WriteLine(xmlTree); //child3.ReplaceWith( // new XElement("NewChild", "new content") //); //Console.WriteLine(xmlTree); #endregion #region XmlDocument Create //System.Xml.XmlDocument xd = new XmlDocument(); //XmlNode root = xd.CreateElement("Response", "www.baidu.com"); //xd.AppendChild(root); //Console.WriteLine(xd.InnerXml); #endregion //XElement xmlTree= Console.Read(); } } }
PurchaseOrder.xml
<?xml version="1.0"?> <PurchaseOrder PurchaseOrderNumber="99503" OrderDate="1999-10-20"> <Address Type="Shipping"> <Name>Ellen Adams</Name> <Street>123 Maple Street</Street> <City>Mill Valley</City> <State>CA</State> <Zip>10999</Zip> <Country>USA</Country> </Address> <Address Type="Billing"> <Name>Tai Yee</Name> <Street>8 Oak Avenue</Street> <City>Old Town</City> <State>PA</State> <Zip>95819</Zip> <Country>USA</Country> </Address> <DeliveryNotes>Please leave packages in shed by driveway.</DeliveryNotes> <Items> <Item PartNumber="872-AA"> <ProductName>Lawnmower</ProductName> <Quantity>1</Quantity> <USPrice>148.95</USPrice> <Comment>Confirm this is electric</Comment> </Item> <Item PartNumber="926-AA"> <ProductName>Baby Monitor</ProductName> <Quantity>2</Quantity> <USPrice>39.98</USPrice> <ShipDate>1999-05-21</ShipDate> </Item> </Items> </PurchaseOrder>
msdn