Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linq to XML Read xml file using linq

Tags:

c#

linq-to-xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Report SYSTEM "https://abc.mycompany.com/abc/processingreports/processeddtd/abcd_1_8.dtd">
<Report Name="Daily TRANSACTIONS"
        Version="1.8"
        xmlns="https://abc.mycompany.com/abc/processingreports/processeddtd/abcd_1_8.dtd"
        OrgID="ABC_PQR" StartDate="2011-03-10T00:00:00+00:00" EndDate="2011-03-11T00:00:00+00:00">
    <Requests>
        <Request ID="2"
                 Date="2011-03-10T00:21:14+00:00"
                 OrderNumber="1">
            <BillTo>
                <FirstName />
            </BillTo>
            <LineItems>
                <LineItem Number="0">
                    <Quantity />
                </LineItem>
            </LineItems>
        </Request>
        <Request ID="2"
                 Date="2011-03-10T00:21:14+00:00"
                 OrderNumber="1">
                 TransactionNumber="389958330911111">
            <BillTo>
                <FirstName>A</FirstName>
            </BillTo>
            <LineItems>
                <LineItem Number="0">
                    <Quantity>1</Quantity>
                </LineItem>
            </LineItems>
            <UniqueData>
                <UniqueNumber>11111111111111111111111111111</UniqueNumber>
            </UniqueData></Request></Requests></Report>

In above XML file using Linq i just want to extract OrderNumber and UniqueNumber OrderNumber="1" 11111111111111111111111111111

Any ideas, suggestions to extract these details?

I can select elements from above xml file but UniqueNumber is not associated with OrderNumber

I am looking for something below (ignore lines where UniqueNumber is not present) OrderNumber - assosicated UniqueNumber

Update In "requiredElements" i am expecting two coulmns OrderNumber and UniqueNumber and holding associated values with each other as 1 and 11111 and so one

 #region FileOpen with UTF8 Encoding

 TextReader sr = new StreamReader(cFileName, Encoding.UTF8);
 XDocument reportfile = XDocument.Load(sr, LoadOptions.SetBaseUri);
 XElement xd = XElement.Parse(reportfile.ToString());
 sr.Close();


 #endregion

 XNamespace ns = xd.Attribute("xmlns").Value;

 var requiredElements = (from resultquery in reportfile.Descendants()
                          select new
                          {
                             OrderNumber = resultquery.Attribute("OrderNumber"),
                             UniqueNumber= (string)resultquery.Element(AddNameSpace(ns, "UniqueNumber")),
                          }
                      );
like image 950
swapneel Avatar asked Jun 28 '11 13:06

swapneel


People also ask

Does LINQ work with XML?

The most important advantage of LINQ to XML is its integration with Language-Integrated Query (LINQ). This integration enables you to write queries on the in-memory XML document to retrieve collections of elements and attributes.

Which of the following option is created to retrieve data into XML using LINQ?

The LINQ to XML will bring the XML document into memory and allows us to write LINQ Queries on in-memory XML document to get the XML document elements and attributes. To use LINQ to XML functionality in our applications, we need to add "System. Xml. Linq" namespace reference.


1 Answers

Here is some sample:

    XDocument doc = XDocument.Load(@"file.xml");
    XNamespace df = doc.Root.Name.Namespace;
    var results = from request in doc.Descendants(df + "Request")
                  where request.Elements(df + "UniqueData").Elements(df + "UniqueNumber").Any()
                  select new
                  {
                      ordNumber = (int)request.Attribute("OrderNumber"),
                      uniqueNumber = (decimal)request.Element(df + "UniqueData").Element(df + "UniqueNumber")
                  };
    foreach (var result in results)
    {
        Console.WriteLine("{0}-{1}", result.ordNumber, result.uniqueNumber);
    }
like image 76
Martin Honnen Avatar answered Oct 02 '22 22:10

Martin Honnen