仅在信息存在时打印节点的值

时间:2012-10-22 00:18:33

标签: xml xpath

我希望只有在节点中有值时才打印出客户的选项。我可以访问信息并将其打印出来,但是,由于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
-------------------------------------------------

1 个答案:

答案 0 :(得分:0)

我想构建你的主循环来遍历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;
}