Domanda

I am trying to write a program that will receive an input from a web service and select the correct information that goes along with the order ID. I am using xPath for this as it is required. For example, if I enter a 1 under the OrderID when I run the program it should pull the totalcost for that order and display it. I know there is definitely something wrong with my code, since I am new to this I have no idea how to physically grab an input when I have no control of the variables being used. Any help would be much appreciated, thanks!

Here is my code:

[WebMethod]
public int GetTotalCostForAnOrder(int OrderID)
{
    XPathNavigator nav;
    XPathDocument docNav;
    XPathNodeIterator NodeIter;

    String rootPath = Server.MapPath("~");
    string strFilename = rootPath + "\\OrderInfoLab3.xml";
    docNav = new XPathDocument(strFilename);

    // Create a navigator to query with XPath.
    nav = docNav.CreateNavigator();
    String searchString = "sum(OrderFeed/Order/Items/Item/TotalCost)";

    // you need to determine the proper XPath statement

    // Select the node and place the results in an iterator.
    NodeIter = nav.Select(searchString);

    while (NodeIter.MoveNext())
    {
        NodeIter.Current.Select("OrderID");
    }

    var totalOrder = nav.Compile(searchString);
    return Convert.ToInt32(nav.Evaluate(totalOrder));
}

Here is my XML File:

<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>

Here is the error I get in my output:

System.Xml.XPath.XPathException: Expression must evaluate to a node-set.
at System.Xml.XPath.XPathNavigator.Select(XPathExpression expr)
at System.Xml.XPath.XPathNavigator.Select(String xpath)
at Lab3.Service1.GetTotalCostForAnOrder(Int32 OrderID) in C:\
È stato utile?

Soluzione

You should use LINQ2XML

public decimal GetTotalCostForAnOrder(int OrderID)
{
    XElement doc=XElement.Load("c:\\yourXML.xml");
    decimal totalPrice=doc.DescendantsAndSelf("Order")
    .Where(x=>x.Attribute("id").Value==OrderID)
    .Select(y=>y.Element("Items")).Elements("Item")
    .Select(z=>decimal.Parse(z.Element("TotalCost").Value)).ToList().Sum();
    return totalPrice;
}

Altri suggerimenti

The following line appears to be incorrect:

String searchString = "sum(OrderFeed/Order/Items/Item/TotalCost)";

This is supposed to be the XPath expression to select the nodes, but there is no OrderFeed node in your XML - the root node is Order.

This should bring better results as a node collection:

String searchString = "/Order/Items/Item/TotalCost";
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top