使用LINQ遍历XML文档

时间:2016-05-17 13:04:51

标签: c# xml linq-to-xml

我正在尝试在下面的XML中读取特定产品(例如Payslips)的地址位置值

              <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>

我有几次失败的尝试。以下代码显示了我的最后一次尝试。能否请你帮忙。提前谢谢。

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



                }
            }

1 个答案:

答案 0 :(得分:0)

如果没有涉及默认命名空间,则以下查询可以正常处理发布的XML:

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")

            };

注意如何将XElement直接投射到适当的类型,例如stringfloat

请参阅下面的演示或在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);
}

输出:

{ distanceInPixelsFromLeftAddr = 76, distanceInPixelsFromBottomAddr = 580, widthAddr = 255, heightAddr = 125 }
相关问题