Search This Blog

Wednesday, December 2, 2015

XDocument C# Example

C# > XML > System.Xml.Linq > XDocument

XDocument is an XML document.

Example


XDocument xDocument = new XDocument(
            new XElement("Hours",
                new XElement("Emp1", "0"),
                new XElement("Emp2", "25"),
                new XElement("Emp3", "40"),
                new XElement("Emp4", "30")
                )
            );

Create a new document by filtering data using Linq

XDocument doc = new XDocument(
           new XElement("Report",
                    from el in xDocument.Element("Hours").Elements()
                    where ((int)el) >=30
                    select el
                )

            );

Result contains two records: