【问题标题】:how do you access a node in a node list that only occurs sometimes c# xml你如何访问节点列表中的节点,有时只会出现c# xml
【发布时间】:2015-06-24 03:00:09
【问题描述】:
    <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 节点集来获取所有 Items,包括 Customer Options 节点集。我想要 PartNo、Description、UnitPrice、Quantity、TotalCost 和 customeroptions(如果有的话)。棘手的部分是我得到了一个例外,因为显然我最终会尝试将未为客户选项列出的数据存储到我制作的结构中。这就是我的网络服务功能。

[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,应该是某种检查,看看是否有可用的 customeroption。我只是想不通它以及之后如何使其正确显示。

【问题讨论】:

    标签: c# asp.net xml xpath logic


    【解决方案1】:

    您需要检查 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;
       }
    

    【讨论】:

    • 这仍然没有遍历 if 语句。此外,当调用该方法时,它只返回循环通过的最后一项。
    • 这不起作用,它不会遍历 if 语句,并且在变量 CustomoptionNode 内部存储了一个空值。此外,输出仍然只显示最后一项。
    • 检查更新的答案。
    • 我得到了同样的结果。它会迭代,但它确实会到达 if 语句并询问该语句的计算结果是否为真。出于某种原因,它没有。
    • 更新:它正在进入 if 语句。它在最后一行得到一个系统空引用异常。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多