使用 LinqToXml 循环 xml
Using LinqToXml to loop through xml
通过 xml;
对 LinqtoXml 进行试验 运行
xml本身看起来像这样
<PurchaseOrders>
<Owner ContactId="39" Owner="M Mouse" Owed="1,609.39" WeeklyDeductionRate="10.00" FromMinimumReturn="110.00" DeductionRate="0.0150" TotalDeductions="34.14" TotalToBeReturned="1,575.24">
<Products ProductId="33" Cost="5.00" Quantity="0.40" />
<Products ProductId="34" Cost="1.80" Quantity="0.90" />
<Products ProductId="41" Cost="2.30" Quantity="1.30" />
<Products ProductId="42" Cost="2.25" Quantity="1.30" />
<Products ProductId="43" Cost="1.60" Quantity="10.50" />
<Products ProductId="57" Cost="7.00" Quantity="13.30" />
<Products ProductId="59" Cost="9.63" Quantity="47.00" />
<Products ProductId="61" Cost="6.23" Quantity="32.60" />
<Products ProductId="66" Cost="1.00" Quantity="5.60" />
<Products ProductId="92" Cost="0.50" Quantity="4.80" />
<Products ProductId="125" Cost="1.00" Quantity="3.80" />
<Products ProductId="139" Cost="6.50" Quantity="3.90" />
<Products ProductId="156" Cost="1.50" Quantity="1.70" />
<Products ProductId="161" Cost="5.80" Quantity="44.20" />
<Products ProductId="171" Cost="3.88" Quantity="12.00" />
<Products ProductId="173" Cost="4.55" Quantity="32.50" />
<Products ProductId="175" Cost="5.00" Quantity="52.90" />
<Products ProductId="182" Cost="0.50" Quantity="18.50" />
<Products ProductId="198" Cost="0.50" Quantity="27.40" />
<Products ProductId="220" Cost="1.50" Quantity="38.60" />
<Products ProductId="231" Cost="6.00" Quantity="0.90" />
<Products ProductId="236" Cost="0.85" Quantity="2.10" />
</Owner>
<Owner ContactId="42" Owner="F Flintstone" Owed="710.01" WeeklyDeductionRate="10.00" FromMinimumReturn="110.00" DeductionRate="0.0150" TotalDeductions="20.65" TotalToBeReturned="689.35">
<Products ProductId="32" Cost="6.00" Quantity="0.50" />
<Products ProductId="33" Cost="5.00" Quantity="2.00" />
<Products ProductId="34" Cost="1.80" Quantity="7.80" />
<Products ProductId="57" Cost="7.00" Quantity="3.10" />
<Products ProductId="59" Cost="10.00" Quantity="16.30" />
<Products ProductId="61" Cost="6.60" Quantity="13.90" />
<Products ProductId="131" Cost="0.90" Quantity="1.70" />
<Products ProductId="156" Cost="1.50" Quantity="1.50" />
<Products ProductId="161" Cost="5.80" Quantity="17.40" />
<Products ProductId="164" Cost="1.10" Quantity="3.10" />
<Products ProductId="171" Cost="3.80" Quantity="5.70" />
我有以下适合时尚的代码:
Dim SupplierId As Integer
Dim lSubTotal As Decimal
Dim lDeductions As Decimal
Dim lToBeReturned As Decimal
Dim lProductId As Integer
Dim xElem = XElement.Load(GenerateStreamFromString(SubmissionsEditor.Text))
' Dim node As XElement
Dim owners = From owner In xElem.Descendants("Owner")
Select owner
For Each owner In owners
SupplierId = CInt(owner.Attribute("ContactId").Value)
lSubTotal = CDec(owner.Attribute("Owed").Value)
lDeductions = CDec(owner.Attribute("TotalDeductions").Value)
lToBeReturned = CDec(owner.Attribute("TotalToBeReturned").Value)
SiAuto.Main.LogInt(NameOf(SupplierId), SupplierId)
SiAuto.Main.LogDecimal(NameOf(lSubTotal), lSubTotal)
SiAuto.Main.LogDecimal(NameOf(lDeductions), lDeductions)
SiAuto.Main.LogDecimal(NameOf(lToBeReturned), lToBeReturned)
SiAuto.Main.LogMessage("Process Purchase Order Header Here")
Dim details = From detail In xElem.Descendants("Products")
Where CInt(owner.Attribute("ContactId").Value) = SupplierId
Select detail
For Each detail In details
lProductId = CInt(detail.Attribute("ProductId").Value)
SiAuto.Main.LogInt(NameOf(lProductId), lProductId)
Next
Next
当我检查日志时,很明显每个所有者元素都在处理,但不是只记录每个所有者元素的 ProductId,然后继续处理下一个所有者,而是记录每个所有者的所有 productId .
我怀疑是这一点出了问题
Dim details = From detail In xElem.Descendants("Products")
Where CInt(owner.Attribute("ContactId").Value) = SupplierId
Select detail
但是是xElem后面的位还是where条件?
谢谢
Descendants
方法 returns 所有子节点的所有 Products
方法,如文档所述 here, on MSDN。这将包括孙子。
如果您想包括特定的产品节点,请优化您的搜索:
Dim details = From detail In owner.Descendants("Products")
Where CInt(owner.Attribute("ContactId").Value) = SupplierId
Select detail
请注意,我选择的是 owner.Descendants
,而不是 xElem.Descendants
。
通过 xml;
对 LinqtoXml 进行试验 运行xml本身看起来像这样
<PurchaseOrders>
<Owner ContactId="39" Owner="M Mouse" Owed="1,609.39" WeeklyDeductionRate="10.00" FromMinimumReturn="110.00" DeductionRate="0.0150" TotalDeductions="34.14" TotalToBeReturned="1,575.24">
<Products ProductId="33" Cost="5.00" Quantity="0.40" />
<Products ProductId="34" Cost="1.80" Quantity="0.90" />
<Products ProductId="41" Cost="2.30" Quantity="1.30" />
<Products ProductId="42" Cost="2.25" Quantity="1.30" />
<Products ProductId="43" Cost="1.60" Quantity="10.50" />
<Products ProductId="57" Cost="7.00" Quantity="13.30" />
<Products ProductId="59" Cost="9.63" Quantity="47.00" />
<Products ProductId="61" Cost="6.23" Quantity="32.60" />
<Products ProductId="66" Cost="1.00" Quantity="5.60" />
<Products ProductId="92" Cost="0.50" Quantity="4.80" />
<Products ProductId="125" Cost="1.00" Quantity="3.80" />
<Products ProductId="139" Cost="6.50" Quantity="3.90" />
<Products ProductId="156" Cost="1.50" Quantity="1.70" />
<Products ProductId="161" Cost="5.80" Quantity="44.20" />
<Products ProductId="171" Cost="3.88" Quantity="12.00" />
<Products ProductId="173" Cost="4.55" Quantity="32.50" />
<Products ProductId="175" Cost="5.00" Quantity="52.90" />
<Products ProductId="182" Cost="0.50" Quantity="18.50" />
<Products ProductId="198" Cost="0.50" Quantity="27.40" />
<Products ProductId="220" Cost="1.50" Quantity="38.60" />
<Products ProductId="231" Cost="6.00" Quantity="0.90" />
<Products ProductId="236" Cost="0.85" Quantity="2.10" />
</Owner>
<Owner ContactId="42" Owner="F Flintstone" Owed="710.01" WeeklyDeductionRate="10.00" FromMinimumReturn="110.00" DeductionRate="0.0150" TotalDeductions="20.65" TotalToBeReturned="689.35">
<Products ProductId="32" Cost="6.00" Quantity="0.50" />
<Products ProductId="33" Cost="5.00" Quantity="2.00" />
<Products ProductId="34" Cost="1.80" Quantity="7.80" />
<Products ProductId="57" Cost="7.00" Quantity="3.10" />
<Products ProductId="59" Cost="10.00" Quantity="16.30" />
<Products ProductId="61" Cost="6.60" Quantity="13.90" />
<Products ProductId="131" Cost="0.90" Quantity="1.70" />
<Products ProductId="156" Cost="1.50" Quantity="1.50" />
<Products ProductId="161" Cost="5.80" Quantity="17.40" />
<Products ProductId="164" Cost="1.10" Quantity="3.10" />
<Products ProductId="171" Cost="3.80" Quantity="5.70" />
我有以下适合时尚的代码:
Dim SupplierId As Integer
Dim lSubTotal As Decimal
Dim lDeductions As Decimal
Dim lToBeReturned As Decimal
Dim lProductId As Integer
Dim xElem = XElement.Load(GenerateStreamFromString(SubmissionsEditor.Text))
' Dim node As XElement
Dim owners = From owner In xElem.Descendants("Owner")
Select owner
For Each owner In owners
SupplierId = CInt(owner.Attribute("ContactId").Value)
lSubTotal = CDec(owner.Attribute("Owed").Value)
lDeductions = CDec(owner.Attribute("TotalDeductions").Value)
lToBeReturned = CDec(owner.Attribute("TotalToBeReturned").Value)
SiAuto.Main.LogInt(NameOf(SupplierId), SupplierId)
SiAuto.Main.LogDecimal(NameOf(lSubTotal), lSubTotal)
SiAuto.Main.LogDecimal(NameOf(lDeductions), lDeductions)
SiAuto.Main.LogDecimal(NameOf(lToBeReturned), lToBeReturned)
SiAuto.Main.LogMessage("Process Purchase Order Header Here")
Dim details = From detail In xElem.Descendants("Products")
Where CInt(owner.Attribute("ContactId").Value) = SupplierId
Select detail
For Each detail In details
lProductId = CInt(detail.Attribute("ProductId").Value)
SiAuto.Main.LogInt(NameOf(lProductId), lProductId)
Next
Next
当我检查日志时,很明显每个所有者元素都在处理,但不是只记录每个所有者元素的 ProductId,然后继续处理下一个所有者,而是记录每个所有者的所有 productId .
我怀疑是这一点出了问题
Dim details = From detail In xElem.Descendants("Products")
Where CInt(owner.Attribute("ContactId").Value) = SupplierId
Select detail
但是是xElem后面的位还是where条件?
谢谢
Descendants
方法 returns 所有子节点的所有 Products
方法,如文档所述 here, on MSDN。这将包括孙子。
如果您想包括特定的产品节点,请优化您的搜索:
Dim details = From detail In owner.Descendants("Products")
Where CInt(owner.Attribute("ContactId").Value) = SupplierId
Select detail
请注意,我选择的是 owner.Descendants
,而不是 xElem.Descendants
。