C# 使用LINQ to XML解析SOAP消息
我正在加快C#中Linq到XML的速度,并试图解析以下消息,但似乎没有取得多大进展。下面是soap消息,我不确定是否需要使用名称空间。下面是我试图格式化的SOAP消息。任何帮助都将不胜感激。我正在尝试提取这些值。谢谢C# 使用LINQ to XML解析SOAP消息,c#,soap,linq-to-xml,C#,Soap,Linq To Xml,我正在加快C#中Linq到XML的速度,并试图解析以下消息,但似乎没有取得多大进展。下面是soap消息,我不确定是否需要使用名称空间。下面是我试图格式化的SOAP消息。任何帮助都将不胜感激。我正在尝试提取这些值。谢谢 <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schem
<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>
1000
测试
I07
17090769
Y
通过调用XDocument.load()
或类似命令,使用Linq的XDocument加载XML文本。然后,您可以使用以下函数遍历xdoc根目录下的元素树
foreach (var x in xdoc.Elements("Lookup"))
{...}
如果这是关于与SOAP服务交互的,请使用 或者 如果这只是解析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,
};
这将使您能够在该方法中将IEnumerable处理为完全强类型对象。您可以将XML转换为XElement,然后只需执行以下操作:
rsp.Descendants("Lookup").ToList();
或
rsp.后代(“objIn”).ToList()
我认为这是最好的办法。我认为XElement是最好的选择。你能发布你的代码吗?或者至少举一个例子来说明这个问题。手动解析SOAP消息有什么特别的原因吗?我相信他需要xdoc.Elements(XName.Get(“lookup”),因为Xml有一个默认的名称空间。非常感谢cameron,你的回答帮了我很多忙,我在一天的搜索后解决了这个问题