如何从Web服务获取输入 - c#

我正在尝试编写一个程序,该程序将从Web服务接收输入,并选择与订单ID一起提供的正确信息。我正在为此使用xPath。例如,如果我在运行程序时在OrderID下输入1,则应该提取该订单的总费用并显示出来。我知道我的代码肯定有问题,因为我是新手,所以我不知道当我无法控制所使用的变量时如何物理地获取输入。任何帮助将不胜感激,谢谢!

这是我的代码:

[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));
}

这是我的XML文件:

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

这是我在输出中得到的错误:

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

参考方案

您应该使用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;
}

jQuery不起作用 - php

我正在使用带有ajax的jquery。有时,给出错误$未定义。这是我的代码:<script language="javascript" type="text/javascript"> var base_path="<? echo $this->baseUrl().'/…

Java中的<<或>>>是什么意思? - java

This question already has answers here: Closed 7 years ago. Possible Duplicate: What does >> and >>> mean in Java?我在一些Java代码中遇到了一些陌生的符号,尽管代码可以正确编译和运行,但对于括号在此代码中的作用却感…

如何以编程方式扩展Jquery Slider Panel? - c#

我有用于登录的JQuery Silder面板|注册选项。供参考,我已包含此滑块Jquery Login | Register Slider Panel的演示实际的问题是,当位于“文字”控件中的“登录”部分或“注册新帐户”部分中有值时,如何以编程方式扩展此滑块。为了理解这个问题,我把我的设计代码。 <asp:Panel ID="Panel_Lo…

Div单击与单选按钮相同吗? - php

有没有一种方法可以使div上的click事件与表单环境中的单选按钮相同?我只希望下面的div提交值,单选按钮很丑代码输出如下:<input id="radio-2011-06-08" value="2011-06-08" type="radio" name="radio_date&#…

菱形运算符<>是否等于<?> - java

我在util.TreeSet类中发现,其中一个构造函数正在使用具有空泛型类型的新TreeMap调用另一个构造函数。 public TreeSet(Comparator<? super E> comparator) { this(new TreeMap<>(comparator)); } new TreeMap<>是什么意思…