Monday, January 26, 2015

LINQ to xml examples



LINQ to xml examples
We have XML
<?xml version="1.0" encoding="UTF-8"?>
<Employees><Employee><EmpId>1</EmpId><Name>Sam</Name><Sex>Male</Sex><Phone Type="Home">423-555-0124</Phone><Phone Type="Work">424-555-0545</Phone>
<Address><Street>7A Cox Street</Street><City>Acampo</City><State>CA</State><Zip>95220</Zip><Country>USA</Country></Address>
</Employee><Employee><EmpId>2</EmpId><Name>Lucy</Name><Sex>Female</Sex><Phone Type="Home">143-555-0763</Phone><Phone Type="Work">434-555-0567</Phone>
<Address><Street>Jess Bay</Street><City>Alta</City><State>CA</State><Zip>95701</Zip><Country>USA</Country></Address>
</Employee><Employee><EmpId>3</EmpId><Name>Kate</Name><Sex>Female</Sex><Phone Type="Home">166-555-0231</Phone><Phone Type="Work">233-555-0442</Phone>
<Address><Street>23 Boxen Street</Street><City>Milford</City><State>CA</State><Zip>96121</Zip><Country>USA</Country></Address>
</Employee><Employee><EmpId>4</EmpId><Name>Chris</Name><Sex>Male</Sex><Phone Type="Home">564-555-0122</Phone><Phone Type="Work">442-555-0154</Phone>
<Address><Street>124 Kutbay</Street><City>Montara</City><State>CA</State><Zip>94037</Zip><Country>USA</Country></Address>
</Employee></Employees>


Using
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
Read the entire XML from D:\Employees.xml
Code:
            XElement xelement = XElement.Load(@"D:\Employees.xml");
            IEnumerable<XElement> employees = xelement.Elements();
            // Read the entire XML
            foreach (var employee in employees)
            {
                Console.WriteLine(employee);
            }
            Console.ReadLine();
Result:
//Read name all of employees
Code:
XElement xelement1 = XElement.Load(@"D:\Employees.xml");
            IEnumerable<XElement> employees2 = xelement1.Elements();
            Console.WriteLine("List of all Employee Names :");
            foreach (var employee in employees2)
            {
                Console.WriteLine(employee.Element("Name").Value);
            }
            Console.ReadLine();
Result:
//Read name and id all of employees
Code:
XElement xelement2 = XElement.Load(@"D:\Employees.xml");
            IEnumerable<XElement> employees3 = xelement2.Elements();
            Console.WriteLine("List of all Employee Names along with their ID:");
            foreach (var employee in employees3)
            {
                Console.WriteLine("{0} has Employee ID {1}",
                    employee.Element("Name").Value,
                    employee.Element("EmpId").Value);
            }
            Console.ReadLine();
Result:
//Read name all of female employees
Code:
XElement xelement3 = XElement.Load(@"D:\Employees.xml");
            var name = from nm in xelement3.Elements("Employee")
                       where (string)nm.Element("Sex") == "Female"
                       select nm;
            Console.WriteLine("Details of Female Employees:");
            foreach (XElement xEle in name)
                Console.WriteLine(xEle);
            Console.ReadLine();
Result:
//Read Home Phone all of employees
Code:
XElement xelement4 = XElement.Load(@"D:\Employees.xml");
            var homePhone = from phoneno in xelement4.Elements("Employee")
                            where (string)phoneno.Element("Phone").Attribute("Type") == "Home"
                            select phoneno;
            Console.WriteLine("List HomePhone Nos.");
            foreach (XElement xEle in homePhone)
            {
                Console.WriteLine(xEle.Element("Phone").Value);
            }
            Console.ReadLine();
Result:
//Find all employees living in 'Alta' City
Code:
XElement xelement5 = XElement.Load(@"D:\Employees.xml");
            var addresses = from address in xelement5.Elements("Employee")
                            where (string)address.Element("Address").Element("City") == "Alta"
                            select address;
            Console.WriteLine("Details of Employees living in Alta City");
            foreach (XElement xEle in addresses)
                Console.WriteLine(xEle);
            Console.ReadLine();
Result:
//Read ZipsCodes all of employees
Code:
XElement xelement6 = XElement.Load(@"D:\Employees.xml");
            Console.WriteLine("List of all Zip Codes");
            foreach (XElement xEle in xelement6.Descendants("Zip"))
            {
                Console.WriteLine((string)xEle);
            }
            Console.ReadLine();
Result:
//Read and sort zip code all of employees
Code:
XElement xelement7 = XElement.Load(@"D:\Employees.xml");
            IEnumerable<string> codes = from code in xelement7.Elements("Employee")
                                        let zip = (string)code.Element("Address").Element("Zip")
                                        orderby zip
                                        select zip;
            Console.WriteLine("List ans sort all zip codes");
            foreach (string zp in codes)
            {
                Console.WriteLine(zp);
            }
            Console.ReadLine();
Result:
//Create an XML Document with Xml Declaration
Code:
XNamespace empNM = "urn:lst-emp:emp";
            XDocument xDoc = new XDocument(
                        new XDeclaration("1.0", "UTF-16", null),
                        new XElement(empNM + "Employees",
                            new XElement("Employee",
                                new XComment("Only 3 elements for demo purposes"),
                                new XElement("EmpId", "5"),
                                new XElement("Name", "Kimmy"),
                                new XElement("Sex", "Female")
                                )));

            StringWriter sw = new StringWriter();
            xDoc.Save(sw);
            Console.WriteLine(sw);
            Console.ReadLine();
Result:
//Save the XML Document to a XMLWriter or to the disk
Code:
XNamespace empNM1 = "urn:lst-emp:emp";
            XDocument xDoc1 = new XDocument(
                        new XDeclaration("1.0", "UTF-16", null),
                        new XElement(empNM1 + "Employees",
                            new XElement("Employee",
                                new XComment("Only 3 elements for demo purposes"),
                                new XElement("EmpId", "5"),
                                new XElement("Name", "Kimmy"),
                                new XElement("Sex", "Female")
                                )));

            StringWriter sw1 = new StringWriter();
            XmlWriter xWrite = XmlWriter.Create(sw1);
            xDoc1.Save(xWrite);
            xWrite.Close();
            // Save to Disk
            xDoc1.Save("D:\\Test.xml");
            Console.WriteLine("Saved");
Result:
Done!!!

0 comments:

Post a Comment