c# - 无法计算出 XPath 计数表达式结果。 C# xml Web 服务

标签 c# asp.net xml xpath

 <?xml version="1.0" encoding="ISO-8859-1" ?> 
- <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>

上面是我正在解析的xml文档。我的目标是使用输入 PartNo,例如“JETSFLAG”是他们传递给应用程序的字符串。我想在 OrderFeed 中查找所有订单,并计算 PartNo 具体被购买的次数。我有一个 XPath Navigator 工具,由于某种原因,当我编写应该工作以获取我想要的内容的表达式时,它不会返回任何内容。

这是我在评估表达式时得到的结果://OrderFeed/Order/Items//Item//PartNo 这让我得到了我需要的数据,我只需要用户输入的任何特定项目的计数...

  <xml>JETSWEATER JETSWEATER JETSFLASHLIGHT JETSWEATER JETSWEATER JETSFLAG JETPANTS JETSWEATER JETSFLAG</xml> 

现在,我很确定编写 count(//OrderFeed/Order/Items//Item//PartNo[JETSFLAG]) 应该会计算 2 个项目。但这就是我得到的返回......

  <xml>0</xml> 

当它显示时,我得到返回 0 的计数。有人可以帮忙吗?

最佳答案

尝试路径:

/OrderFeed/Order/Items/Item[PartNo=JETSFLAG]

(根据您的环境引用)

关于c# - 无法计算出 XPath 计数表达式结果。 C# xml Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29710046/

相关文章:

c# - 传输编码:在 Windows Phone 中分块

asp.net - 如何更改生产 .NET 网站中的 Web 引用?

c# - 检索包含混合内容的完整 XElement 字符串

xml - 找出 XPath 的工具

xml - 如何提取一个值并在将来的 xpath 查询中使用它?

c# - 如何在 WPF 中设置进度条的可见性?

c# - 剪刀石头布c#登场如何显示胜率?

c# - Auto Property - Resharper 错误或酷的未知 C# 功能?

asp.net - 将存储库模式与 Entity Framework (mvc 店面)一起使用

asp.net - ASP .NET 中 Url 编码的权威指南