2010-12-01 97 views
4

我正在加快Linq到C#中的XML并试图解析以下消息,似乎没有取得很大进展。这里是肥皂消息我不确定我是否需要使用命名空间。这是我想要格式化的SOAP消息。任何帮助将不胜感激。我正在尝试提取值。谢谢。使用LINQ to XML来解析SOAP消息

<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> 
<soap:Body> 
    <Lookup xmlns="http://ASR-RT/"> 
    <objIn> 
    <transactionHeaderData> 
    <intWebsiteId>1000</intWebsiteId> 
    <strVendorData>test</strVendorData> 
    <strVendorId>I07</strVendorId> 
    </transactionHeaderData> 
    <intCCN>17090769</intCCN> 
    <strSurveyResponseFlag>Y</strSurveyResponseFlag> 
    </objIn> 
    </CCNLookup> 
</soap:Body> 
</soap:Envelope> 
+0

你可以发布你有的代码吗?或者至少是一个演示问题的例子。 – 2010-12-01 22:34:39

+0

手动分析SOAP消息的任何特定原因? – 2010-12-01 22:34:56

回答

0

使用Linq的XDocument通过调用XDocument.Load()或类似方法来加载XML文本。然后你就可以通过关闭XDOC的根元素的树走,使用功能,如

foreach (var x in xdoc.Elements("Lookup")) 
{...} 
+0

我相信他需要xdoc.Elements(XName.Get(“lookup”,“http:// ASR-RT /”)),因为Xml有一个默认命名空间 – dkackman 2010-12-01 23:02:11

9

如果这是关于与SOAP服务交互,请使用 Add Service Referencewsdl.exe

如果这只是解析XML,假设你已经得到了SOAP响应到名为soapDocument一个XDocument:

XNamespace ns = "http://ASR-RT/"; 
var objIns = 
    from objIn in soapDocument.Descendants(ns + "objIn") 
    let header = objIn.Element(ns + "transactionHeaderData") 
    select new 
    { 
     WebsiteId = (int) header.Element(ns + "intWebsiteId"), 
     VendorData = header.Element(ns + "strVendorData").Value, 
     VendorId = header.Element(ns + "strVendorId").Value, 
     CCN = (int) objIn.Element(ns + "intCCN"), 
     SurveyResponse = objIn.Element(ns + "strSurveyResponseFlag").Value, 
    }; 

这将会给你的你处理的完全强类型对象anonymous types一个IEnumerable在该方法内。

0

你可以得到你的XML到的XElement的,然后就去做。

​​

或者

rsp.Descendants( “objIn”)ToList();

我认为这是做到这一点的最佳方式。我认为XElement是最好的选择。