我试图从下面的SOAP
获取faultcode,faultstring和OrderNumber的值<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body>
<faultcode>1234</faultcode>
<faultstring>SaveOrder:SetrsOrderMain:Cannot change OrderDate if GLPeriod is closed, new OrderDate is 3/2/2010:Ln:1053</faultstring>
<detail>
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP:Body UserGUID="test">
<m:SaveOrder xmlns:m="http://www.test.com/software/schema/" UserGUID="test">
<Order OrderNumber="1234-1234-123" Caller="" OrderStatus="A" xmlns="http://www.test.com/software/schema/">
这是我在C#中的代码
XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath("XMLexample.xml"));
var errorDetail = new EcourierErrorDetail
{
FaultCode = from fc in doc.Descendants("faultcode")
select fc.Value,
FaultString = from fs in c.Descendants("faultstring")
select fs.Value,
OrderNumber = from o in
doc.Descendants("detail").Elements("Order").Attributes("OrderNumber")
select o.Value
};
return errorDetail;
我能够获取faultcode和faultstring的值,但不能获取OrderNumber的值。我得到“枚举没有结果。”有人可以帮忙吗?感谢。
答案 0 :(得分:6)
是的,您在选择时忽略了XML命名空间:
<SOAP:Envelope xmlns:SOAP="http://schemas.xmlsoap.org/soap/envelope/">
.....
<m:SaveOrder xmlns:m="http://www.test.com/software/schema/" UserGUID="test">
<Order OrderNumber="1234-1234-123" Caller="" OrderStatus="A" xmlns="http://www.test.com/software/schema/">
<Order>
标记位于<m:SaveOrder>
标记内,该标记使用前缀为m:
前缀的XML命名空间。
此外,您正在尝试选择“详细信息”,然后直接跳到“订单”节点(使用.Elements()
) - 您错过了中间的<m:SaveOrder>
节点。
选择时需要考虑到这一点:
XDocument doc = XDocument.Load(HttpContext.Current.Server.MapPath("XMLexample.xml"));
XNamespace xmlns = "http://www.test.com/software/schema/";
var orderNode = doc.Descendants(xmlns + "SaveOrder").Elements(xmlns + "Order");
var value = from o in orderNode.Attributes("OrderNumber")
select o.Value;
这会给你一个结果??