2016-08-17 81 views
1

我正在尝试读取此xml消息中的APP_DATE元素。使用LINQ尝试,但无法读取,因为你没有提供任何示例代码,但我相信你,因为命名空间的斗争MyResult从SOAP消息中读取元素

<?xml version="1.0" encoding="utf-8" ?> 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
<soap:Body> 
<MyResponse xmlns="http://tempuri.org/"> 
<MyResult> 
    <APP_TYPE>I</APP_TYPE> 
    <APP_NO>152240</APP_NO> 
    <APP_DATE>10/03/2016</APP_DATE> 
    </MyResult> 
    </MyResponse> 
    </soap:Body> 
</soap:Envelope> 

回答

1

不能肯定。所以,试试这个:

XmlDocument doc = new XmlDocument(); 
doc.Load("data.xml"); 
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); 
ns.AddNamespace("soap", "http://www.w3.org/2003/05/soap-envelope"); 
ns.AddNamespace("x", "http://tempuri.org/"); 

var result = doc.SelectSingleNode("//soap:Envelope/soap:Body/x:MyResponse/x:MyResult/x:APP_DATE", ns).InnerText; 

为了更深入的了解,你可以阅读this question

使用LINQ这将是这样的:

var result = XDocument.Load("data.xml").Root 
         .Descendants(XName.Get("APP_DATE", "http://tempuri.org/")) 
         .FirstOrDefault()?.Value; 

了解如何在这两个例子中我有指定的命名空间我在找什么

+0

“?”是什么意思?在你的linq querry做什么? – gismo

+1

@ gismo - 它是c#6.0的'x == null的语法糖吗?/* x */x.Value类型的默认值。你可以在[MSDN](https://msdn.microsoft.com/en-us/magazine/dn802602.aspx)中准备好它, –