尝试使用LinqtoXml来运行某些xml;
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&# 39;每个所有者都有。
我怀疑这一点是错误的
Dim details = From detail In xElem.Descendants("Products")
Where CInt(owner.Attribute("ContactId").Value) = SupplierId
Select detail
然而,它是否跟随xElem或where条件?
由于
答案 0 :(得分:1)
Descendants
方法返回所有子节点上的所有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
。