簡體   English   中英

使用LINQ遍歷XML文檔

[英]Traversing XML document using LINQ

我正在嘗試在下面的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);



                }
            }

如果沒有涉及默認命名空間,則以下查詢可以正常處理發布的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 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM