我有一些像这样的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 doc As New XmlDocument()
doc.Load(GenerateStreamFromString(SubmissionsEditor.Text))
Dim elemList As XmlNodeList = doc.GetElementsByTagName("Owner")
Dim pElemList As XmlNodeList = doc.GetElementsByTagName("Products")
For i As Integer = 0 To elemList.Count - 1
Dim lSupplierId As Integer = CInt(elemList(i).Attributes("ContactId").Value)
SiAuto.Main.LogInt(NameOf(lSupplierId), lSupplierId)
For j As Integer = 0 To pElemList.Count - 1
Dim lProductId As Integer = CInt(pElemList(i).Attributes("ProductId").Value)
SiAuto.Main.LogInt("Number of Products that will be looped Through", pElemList.Count - 1)
SiAuto.Main.LogInt(NameOf(lProductId), lProductId)
Next
Next
我对生成的日志文件感到吃惊,因为我期望循环的产品数量达到22个。显然,循环结构的一部分是不正确的,有些人可以指向我?
由于
答案 0 :(得分:2)
您的循环实际上正在运行所有产品元素,因为您的代码详细说明了所有者与其产品之间没有任何关联。如果您更喜欢当前的XPath方法,则应选择当前所有者元素的子元素的所有产品元素:
Dim doc As New XmlDocument()
doc.Load(GenerateStreamFromString(SubmissionsEditor.Text))
Dim elemList As XmlNodeList = doc.GetElementsByTagName("Owner")
For i As Integer = 0 To elemList.Count - 1
Dim pElemList As XmlNodeList = elemList(i).SelectNodes("//Products")
Dim lSupplierId As Integer = CInt(elemList(i).Attributes("ContactId").Value)
SiAuto.Main.LogInt(NameOf(lSupplierId), lSupplierId)
For j As Integer = 0 To pElemList.Count - 1
Dim lProductId As Integer = CInt(pElemList(j).Attributes("ProductId").Value)
SiAuto.Main.LogInt("Number of Products that will be looped Through", pElemList.Count - 1)
SiAuto.Main.LogInt(NameOf(lProductId), lProductId)
Next
Next
如果您更喜欢在实现中使用XPath和For Each循环(这有助于避免错误的索引拼写错误),您的代码可能如下所示:
Dim doc As New XmlDocument()
doc.Load(GenerateStreamFromString(SubmissionsEditor.Text))
Dim elemList As XmlNodeList = doc.SelectNodes("//Owner")
For Each elem As XmlNode In elemList
Dim pElemList As XmlNodeList = elem.SelectNodes("//Products")
Dim lSupplierId As Integer = CInt(elem.Attributes("ContactId").Value)
SiAuto.Main.LogInt(NameOf(lSupplierId), lSupplierId)
For Each pElem As XmlNode In pElemList
Dim lProductId As Integer = CInt(pElem.Attributes("ProductId").Value)
SiAuto.Main.LogInt("Number of Products that will be looped Through", pElemList.Count - 1)
SiAuto.Main.LogInt(NameOf(lProductId), lProductId)
Next
Next