3
我正在寻找只有在节点中有值时才打印出客户的选项。我可以访问信息并将其打印出来,但是,由于while循环,它会在每个帐户下打印出信息。任何帮助或暗示将不胜感激,谢谢。仅在信息存在的情况下打印节点的值
下面是访问XML代码:
XPathNodeIterator ItemOptionsIter;
String ItemsearchStringOptions = "Order/Items/Item/CustomerOptions";
ItemOptionsIter = nav.Select(ItemsearchStringOptions);
if (ItemIter.Current.SelectSingleNode("CustomerOptions") != null)
{
while (ItemOptionsIter.MoveNext())
{
XPathNodeIterator ItemOptions = ItemOptionsIter.Current.SelectChildren(XPathNodeType.Element);
if (ItemOptions.Current.HasChildren)
{
txtItemInfo.Text = txtItemInfo.Text + "Size: " + ItemOptions.Current.SelectSingleNode("Size") + Environment.NewLine;
txtItemInfo.Text = txtItemInfo.Text + "Color: " + ItemOptions.Current.SelectSingleNode("Color") + Environment.NewLine;
txtItemInfo.Text = txtItemInfo.Text + "-------------------------------------------------" + Environment.NewLine;
}
}
}
下面是XML文件:
<Item>
<PartNo>JETSWEATER</PartNo>
<Description>N.Y. Jets Sweatshirt</Description>
<UnitPrice>10.50</UnitPrice>
<Quantity>2</Quantity>
<TotalCost>21.00</TotalCost>
<CustomerOptions>
<Size>M</Size>
<Color>Green</Color>
</CustomerOptions>
</Item>
<Item>
<PartNo>JETSSWEATER</PartNo>
<Description>N.Y. Jets Sweatshirt</Description>
<UnitPrice>7.50</UnitPrice>
<Quantity>3</Quantity>
<TotalCost>22.50</TotalCost>
<CustomerOptions>
<Size>S</Size>
<Color>White</Color>
</CustomerOptions>
</Item>
<Item>
<PartNo>JETSFLAG</PartNo>
<Description>N.Y. Jets Flag for display</Description>
<UnitPrice>5.00</UnitPrice>
<Quantity>1</Quantity>
<TotalCost>5.00</TotalCost>
<CustomerOptions/>
</Item>
最后这里是我的输出的一个示例:
Part Number: JETSWEATER Description: N.Y. Jets Sweatshirt UnitPrice: 10.50 Quantity: 2 TotalCost: 21.00 ------------------------------------------------- Size: M Color: Green ------------------------------------------------- Size: S Color: White ------------------------------------------------- Part Number: JETSSWEATER Description: N.Y. Jets Sweatshirt UnitPrice: 7.50 Quantity: 3 TotalCost: 22.50 ------------------------------------------------- Size: M Color: Green ------------------------------------------------- Size: S Color: White ------------------------------------------------- Part Number: JETSFLAG Description: N.Y. Jets Flag for display UnitPrice: 5.00 Quantity: 1 TotalCost: 5.00 ------------------------------------------------- Size: M Color: Green ------------------------------------------------- Size: S Color: White -------------------------------------------------
试着改变你的XPath表达式'订单/项目/产品/ CustomerOptions [布尔(*)]' – Pawel
@ nate00234做了上述工作适合你,或者你还在阻碍? – JWiley