linq to xml samples
Using XPath ---------------------- var customers = XDocument.Load(@"c:\customers.xml"); var Companies = customers.XPathSelectElements("//Customer[Country='USA']"); or var Companies = customers.XPathSelectElements("//Customer[@CustomerID='ABC']"); foreach (var item in Companies) { Console.WriteLine("CustomerID: " item.Attribute("CustomerID").Value " Company : " item.Element("Company").Value " ContactName : " item.Element("ContactName").Value ); } Basic Xml Creation --------------------------------- var customers = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XComment("Sample xml document"), new XElement("Customers", new XElement("Customer", new XAttribute("CustomerID", "MICRO"), new XElement("Company", "Microsoft"), new XElement("ContactName", "Andrew"), new XElement("Country", "USA"), new XElement("Orders", new XElement("Order", new XAttribute("OrderID", "10643") ) ) ) ) ); customers.Save(@"c:\customers.xml"); Set and Remove Values -------------------------------------- var customers = XDocument.Load(@"c:\customers.xml"); var Companies = customers.XPathSelectElement("//Customer[@CustomerID='ABC']"); var mobile = new XElement("Mobile","555 55 55"); Companies.Add(mobile); Companies.Elements("Orders").Remove(); Companies.SetElementValue("Fax", "555 66 55"); //Removes the element Companies.SetElementValue("Phone", null); //sets the element value Companies.Element("Fax").Value = "444 55 44"; XSD Validation ---------------------------- var customers = XDocument.Load(@"c:\customers.xml"); XmlSchemaSet schemas = new XmlSchemaSet(); schemas.Add(null, @"c:\customers.xsd"); try { customers.Validate(schemas, null); Console.WriteLine("validation successful!"); } catch (Exception ex) { Console.WriteLine(ex.Message); } Filtering Xml ---------------------- var customers = XDocument.Load(@"c:\customers.xml"); var customersInUSA = from cust in customers.Descendants("Customer") where cust.Element("Country").Value == "USA" select cust; foreach (var item in customersInUSA) { Console.WriteLine("Company : " item.Element("Company").Value " Contact name: " item.Element("ContactName").Value ); } ========================================================== var customers = XDocument.Load(@"c:\customers.xml"); var customersInUSA = from cust in customers.Descendants("Customer") where cust.Element("Country").Value == "USA" select new { CustomerID = cust.Attribute("CustomerID").Value, Company = cust.Element("Company").Value, Phone = cust.Element("Phone").Value }; foreach (var item in customersInUSA) { Console.WriteLine("CustomerID : " item.CustomerID " Company: " item.Company " Phone: " item.Phone ); } ============================================================ var customers = XDocument.Load(@"c:\customers.xml"); var results = from c in customers.Descendants("Customer") from o in c.Descendants("Orders") let Count = o.Elements().Count() orderby Count descending where o.Elements().Any() select new { Company = c.Element("Company").Value, Count }; foreach (var item in results) { Console.WriteLine(item.Company ": " item.Count); } ================================================================= var customers = XDocument.Load(@"c:\customers.xml"); var results = from c in customers.Descendants("Customer") from o in c.Descendants("Orders") let Count = o.Elements().Count() orderby Count descending where o.Elements().Any( order=>order.Element("ShipVia").Value=="Federal" ) select new { Company = c.Element("Company").Value, Count }; foreach (var item in results) { Console.WriteLine(item.Company ": " item.Count); } Transform XML -------------------------- var customers = XDocument.Load(@"c:\customers.xml"); var contacts = new XElement("Contacts", from c in customers.Descendants("Customer") select new XElement("Contact", new XElement("Name",c.Element("ContactName").Value), c.Element("Company"), new XElement("Phone", new XAttribute("type","Work"), c.Element("Phone").Value), new XElement("Phone", new XAttribute("type","Fax"), c.Element("Fax").Value ) ) ); Console.WriteLine(contacts);
Comments