如何访问节点列表中有时仅出现c#xml的节点

时间:2015-04-17 10:02:45

标签: c# asp.net xml xpath logic

    <OrderFeed>
<Order id="1">
    <BillingInformation>
        <Name>Bruce Ganek</Name>
        <Address>99 Main Street</Address>
        <City>Cranston</City>
        <State>RI</State>
        <ZipCode>02910</ZipCode>
    </BillingInformation>
    <ShippingInformation>
        <Name>Governor Chafee</Name>
        <Address>82 Smith St # 115</Address>
        <City>Providence</City>
        <State>RI</State>
        <ZipCode>02903-1121</ZipCode>
    </ShippingInformation>
    <Items>
        <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>JETSWEATER</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>JETSFLASHLIGHT</PartNo>
            <Description>N.Y. Jets Flashlight</Description>
            <UnitPrice>5.00</UnitPrice>
            <Quantity>1</Quantity>
            <TotalCost>5.00</TotalCost>

            <CustomerOptions/>

        </Item>

    </Items>
</Order>
<Order id="2">
    <BillingInformation>
        <Name>Walt Disney</Name>
        <Address>DisneyWorld Hotel</Address>
        <City>Orlando</City>
        <State>FL</State>
        <ZipCode>32801</ZipCode>
    </BillingInformation>
    <ShippingInformation>
        <Name>Walt Disney</Name>
        <Address>DisneyWorld Hotel</Address>
        <City>Orlando</City>
        <State>FL</State>
        <ZipCode>32801</ZipCode>
    </ShippingInformation>
    <Items>
        <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>JETSWEATER</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>

    </Items>
</Order>
<Order id="3">
    <BillingInformation>
        <Name>Tom Brady</Name>
        <Address>One Patriot Place</Address>
        <City>Foxboro</City>
        <State>MA</State>
        <ZipCode>02035</ZipCode>
    </BillingInformation>
    <ShippingInformation>
        <Name>Tom Brady</Name>
        <Address>2121 George Halas Drive</Address>
        <City>Canton</City>
        <State>OH</State>
        <ZipCode>44708</ZipCode>
    </ShippingInformation>
    <Items>
        <Item>
            <PartNo>JETPANTS</PartNo>
            <Description>N.Y. Jets Sweatpants</Description>
            <UnitPrice>10.50</UnitPrice>
            <Quantity>3</Quantity>
            <TotalCost>31.50</TotalCost>
            <CustomerOptions>
                <Size>M</Size>
                <Color>Green</Color>
            </CustomerOptions>
        </Item>
        <Item>
            <PartNo>JETSWEATER</PartNo>
            <Description>N.Y. Jets Sweatshirt</Description>
            <UnitPrice>7.50</UnitPrice>
            <Quantity>1</Quantity>
            <TotalCost>7.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>

    </Items>
</Order>
</OrderFeed>

我正在尝试访问asp.net Web服务应用程序中的客户选项。我无法弄清楚如何使用XPath表达式遍历Item节点集以获取包括Customer Options节点集在内的所有Items。如果有任何项目,我想要PartNo,Description,UnitPrice,Quantity,TotalCost,然后是客户。棘手的部分是我得到一个例外,因为显然我最终会尝试将未列入客户选项的数据存储到我制作的结构中。这就是我在Web服务中的功能。

[WebMethod]
    public lab3 GetItemListForOrder(int OrderID)
    {

        string strFileName = Server.MapPath("~\\XML_OrderInfo_Lab3.xml");
        XPathDocument xDoc = new XPathDocument(strFileName);
        //XPathNodeIterator NodeIter;
        XPathNavigator nav;
        XPathNodeIterator NodeIter;

        nav = xDoc.CreateNavigator();
        string searchstring = "//OrderFeed/Order[ " + OrderID + "]/Items//Item";

        NodeIter = nav.Select(searchstring);
        StringBuilder sb = new StringBuilder();

        while (NodeIter.MoveNext())
        {
            lab3 lab = new lab3();
            XPathNavigator Items = NodeIter.Current;
            lab.PartNO = Items.SelectSingleNode("PartNo").ToString();
            lab.Description = Items.SelectSingleNode("Description").ToString();
            lab.UnitPrice = Items.SelectSingleNode("UnitPrice").ToString();
            lab.TotalCost = Items.SelectSingleNode("TotalCost").ToString();

            lab.Size = Items.SelectSingleNode("Size").ToString();
            sb.Append(Items.SelectSingleNode("Color").ToString() + Environment.NewLine);
            return lab;
        }
    }

实验室之间。 TotalCost line和lab.Size应该进行某种检查以确定是否有可用的客户。我无法弄明白,以及如何在事后正确显示。

1 个答案:

答案 0 :(得分:0)

您需要检查CustomOptions

while (NodeIter.MoveNext())
        {
            lab3 lab = new lab3();
            XPathNavigator Items = NodeIter.Current;
            lab.PartNO = Items.SelectSingleNode("PartNo").ToString();
            lab.Description = Items.SelectSingleNode("Description").ToString();
            lab.UnitPrice = Items.SelectSingleNode("UnitPrice").ToString();
            lab.TotalCost = Items.SelectSingleNode("TotalCost").ToString();

          var CustomoptionNode = Items.Select("/CustomerOptions");
          string Color = string.Empty;
           if(CustomoptionNode != null)
       {
            lab.Size = CustomoptionNode .SelectSingleNode("Size").ToString();
            Color = CustomoptionNode .SelectSingleNode("Color").ToString();

      }


   sb.Append(Color  + Environment.NewLine);

            return lab;
   }