开发者

Linq to XML Read xml file using linq

<?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 UniqueNum开发者_开发技巧ber 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")),
                          }
                      );


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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜