I'm looking to print out a customer's options only if they have a value in the node. I can access the information and print it out, however, because of the while loop it prints out the information under each account. Any help or hints would be appreciated, thanks.
我希望只有在节点中有值时才打印出客户的选项。我可以访问该信息并将其打印出来,但是,由于while循环,它会打印出每个帐户下的信息。任何帮助或提示将不胜感激,谢谢。
Here is the code to access the xml:
以下是访问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;
}
}
}
Here is the xml file:
这是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>
And finally here is a sample of my output:
最后这是我输出的一个示例:
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 -------------------------------------------------
1 个解决方案
#1
0
I'd say construct your main loop to iterate through the Item nodes and print out their values, and then in the place where you would print out the options, do this:
我会说构造你的主循环来迭代Item节点并打印出它们的值,然后在打印选项的地方,执行以下操作:
XPathNodeIterator options = item.SelectNodes("CustomerOptions/*[. != '']");
if(options.Count != 0)
{
XPathNavigator size = item.SelectSingleNode("CustomerOptions/Size[. != '']");
if(size != null)
{
txtItemInfo.Text = txtItemInfo.Text + "Size: " + size.Value + Environment.NewLine;
}
// Then do the same for color
txtItemInfo.Text = txtItemInfo.Text + "-------------------------------------------------" + Environment.NewLine;
}
#1
0
I'd say construct your main loop to iterate through the Item nodes and print out their values, and then in the place where you would print out the options, do this:
我会说构造你的主循环来迭代Item节点并打印出它们的值,然后在打印选项的地方,执行以下操作:
XPathNodeIterator options = item.SelectNodes("CustomerOptions/*[. != '']");
if(options.Count != 0)
{
XPathNavigator size = item.SelectSingleNode("CustomerOptions/Size[. != '']");
if(size != null)
{
txtItemInfo.Text = txtItemInfo.Text + "Size: " + size.Value + Environment.NewLine;
}
// Then do the same for color
txtItemInfo.Text = txtItemInfo.Text + "-------------------------------------------------" + Environment.NewLine;
}