gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 14:44:18 25 4
gpt4 key购买 nike

我正在尝试编写一个程序,该程序将从网络服务接收输入并选择与订单 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:\

最佳答案

以下行似乎不正确:

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

这应该是选择节点的 XPath 表达式,但 XML 中没有 OrderFeed 节点 - 根节点是 Order

作为节点集合,这应该会带来更好的结果:

String searchString = "/Order/Items/Item/TotalCost";

关于c# - 如何从网络服务获取输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13102436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com