使用 LINQ 遍历 XML 文档
Traversing XML document using LINQ
我正在尝试读取以下特定产品(例如工资单)的地址位置值 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>
我有几次尝试都失败了。下面的代码显示了我最后一次尝试。能否请你帮忙。提前致谢。
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
直接转换为适当的类型,例如 string
或 float
。
请参阅下面的演示或在 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 }
我正在尝试读取以下特定产品(例如工资单)的地址位置值 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>
我有几次尝试都失败了。下面的代码显示了我最后一次尝试。能否请你帮忙。提前致谢。
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
直接转换为适当的类型,例如 string
或 float
。
请参阅下面的演示或在 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 }