简体   繁体   中英

Traversing XML document using LINQ

I'm trying to read in the address position values of a specific product (eg Payslips) in the below XML

              <INI>
              <ReportTemplate>report_template_land.pdf</ReportTemplate>
              <ReportAccountID>Reports</ReportAccountID>
              <!--Table for sending the documents to different channels-->
              <ChannelDeliveryTable>ChannelDeliveryTable.csv</ChannelDeliveryTable>
              <Documents>
              <Payslip>
                      <Address>
                            <distanceInPixelsFromLeft>76</distanceInPixelsFromLeft>
                            <distanceInPixelsFromBottom>580</distanceInPixelsFromBottom>
                            <width>255</width>
                            <height>125</height>
                      </Address>
             </Payslip>
                  <Invoice>
                    <Address>
                        <distanceInPixelsFromLeft>65</distanceInPixelsFromLeft>
                        <distanceInPixelsFromBottom>580</distanceInPixelsFromBottom>
                        <width>255</width>
                    <height>125</height>
              </Address>
                </Invoice>
                </Documents>
                </INI>

I had couple of attempts which all failed. The below code shows my last attempt. Could you please help. Thanks in advance.

            float distanceInPixelsFromLeftAddr;
            float distanceInPixelsFromBottomAddr;
            float widthAddr;
            float heightAddr;
            try
            {
                //var addrPos = from xml in XmlDoc.Elements("Payslip").Descendants("Address")
                var addrPos = from xml in XmlDoc.Descendants("Payslip").Descendants("Address")
                              select new

                              {

                                  distanceInPixelsFromLeftAddr = xml.Element("distanceInPixelsFromLeft").Value,
                                  distanceInPixelsFromBottomAddr = xml.Element("distanceInPixelsFromBottom").Value,
                                  widthAddr = xml.Element("width").Value,
                                  heightAddr = xml.Element("height").Value

                              };

                foreach (var node in addrPos)
                {

                    distanceInPixelsFromLeftAddr = float.Parse(node.distanceInPixelsFromLeftAddr);
                        distanceInPixelsFromBottomAddr = float.Parse(node.distanceInPixelsFromBottomAddr);
                        widthAddr = float.Parse(node.widthAddr);
                        heightAddr = float.Parse(node.heightAddr);



                }
            }

Given no default namespace is involved, the following query works just fine against the XML posted in question :

var addrPos = from xml in XmlDoc.Descendants("Payslip").Elements("Address")
            select new
            {

                distanceInPixelsFromLeftAddr = (string)xml.Element("distanceInPixelsFromLeft"),
                distanceInPixelsFromBottomAddr = (string)xml.Element("distanceInPixelsFromBottom"),
                widthAddr = (float)xml.Element("width"),
                heightAddr = (float)xml.Element("height")

            };

Notice how you can cast XElement directly to the appropriate type such as string or float .


See the demo below or see it live in dotnetfiddle :

var raw = @"<INI> 
  <ReportTemplate>report_template_land.pdf</ReportTemplate>  
  <ReportAccountID>Reports</ReportAccountID>  
  <!--Table for sending the documents to different channels-->  
  <ChannelDeliveryTable>ChannelDeliveryTable.csv</ChannelDeliveryTable>  
  <Documents> 
    <Payslip> 
      <Address> 
        <distanceInPixelsFromLeft>76</distanceInPixelsFromLeft>  
        <distanceInPixelsFromBottom>580</distanceInPixelsFromBottom>  
        <width>255</width>  
        <height>125</height> 
      </Address> 
    </Payslip>  
    <Invoice> 
      <Address> 
        <distanceInPixelsFromLeft>65</distanceInPixelsFromLeft>  
        <distanceInPixelsFromBottom>580</distanceInPixelsFromBottom>  
        <width>255</width>  
        <height>125</height> 
      </Address> 
    </Invoice> 
  </Documents> 
</INI>
";
var XmlDoc = XDocument.Parse(raw);

var addrPos = from xml in XmlDoc.Descendants("Payslip").Elements("Address")
    select new
{
    distanceInPixelsFromLeftAddr = (string)xml.Element("distanceInPixelsFromLeft"),
    distanceInPixelsFromBottomAddr = (string)xml.Element("distanceInPixelsFromBottom"),
    widthAddr = (float)xml.Element("width"),
    heightAddr = (float)xml.Element("height")

};

foreach (var node in addrPos)
{

    Console.WriteLine(node);
}

Output :

{ distanceInPixelsFromLeftAddr = 76, distanceInPixelsFromBottomAddr = 580, widthAddr = 255, heightAddr = 125 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM