【发布时间】:2014-02-22 08:07:44
【问题描述】:
我一直在尝试解析一些信息并遵循http://lakenine.com/reading-xml-with-namespaces-using-linq/ 的精彩建议,我确信这已经接近了,但我没有得到任何结果显示。没有错误,只是没有结果。断点和检查变量表明 docx 具有正确的信息,但我的 for 循环被跳过了。我玩过多种变体,只设法使代码崩溃。我相信问题出在 XPathSelectElements 参数上,但不知道还能尝试什么。 在这个阶段,我需要的只是令牌,但我需要稍后重用代码来返回可能有多个结果的结果。请提前告知并感谢您:
string sampleXML = String.Concat(
"<soap12:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"",
" xmlns:soap12=\"http://www.w3.org/2003/05/soap-envelope\">",
" <soap12:Body>",
" <BeginSessionV2Response xmlns=\"http://ws.jobboard.com/jobs/\">",
" <BeginSessionV2Result>ca5522fb93ef499f8ed010a5f4153af7-446298346-SB-4</BeginSessionV2Result>",
" </BeginSessionV2Response>",
" </soap12:Body>",
" </soap12:Envelope>"
);
XmlReader reader = XmlReader.Create(new StringReader(sampleXML));
System.Xml.XmlNameTable nameTable = reader.NameTable;
System.Xml.XmlNamespaceManager namespaceManager = new System.Xml.XmlNamespaceManager(nameTable);
namespaceManager.AddNamespace("soap12", "http://www.w3.org/2001/XMLSchema-instance/");
XElement docx = XElement.Load(reader);
string vbResultz = "start: ";
var sessionKey = from pn
in docx.XPathSelectElements("soap12:Body/BeginSessionV2Response/BeginSessionV2Result", namespaceManager)
select (string)pn;
foreach (string pn in sessionKey)
{
vbResultz += pn;
}
ViewBag.REsultz = vbResultz;
return View();
}
【问题讨论】:
标签: c# asp.net-mvc-4 soap