【发布时间】:2009-10-18 12:55:02
【问题描述】:
试图解析一些 XML,但显然这对于一个慵懒的周日下午来说太过分了,
这是我的代码:(我也尝试了 XPathDocument 和 XmlDocument 方法,但这也失败了)
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(postData);
XDocument xDoc = XDocument.Load(new XmlNodeReader(xmlDoc));
XNamespace soapEnv = "http://schemas.xmlsoap.org/soap/envelope/";
XElement xEnv = xDoc.Descendants(soapEnv + "Envelope").First();
XElement xBody = xEnv.Descendants(soapEnv + "Body").First();
XElement xReadReply = xBody.Descendants("ReadReplyReq").First();
最后一行失败,异常:此集合中没有元素 但是,如果我将最后一行更改为:
XElement xReadReply = xBody.Descendants().First();
它返回第一个节点,实际上是“ReadReplyReq”节点。
终于让这些命名空间工作了,它现在在没有命名空间的第一个节点上失败了......哦,讽刺的讽刺;^)
这是我要解析的 XML:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header>
<TransactionID xmlns="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2" SOAP-ENV:actor="" SOAP-ENV:mustUnderstand="1">12345678</TransactionID>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<ReadReplyReq xmlns="http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2">
<MMStatus>Read</MMStatus>
<TimeStamp>2007-12-13T14:05:27+01:00</TimeStamp>
<MessageID>54321</MessageID>
<Sender>
<ShortCode>+12345</ShortCode>
</Sender>
<Recipient>
<Number>+12345</Number>
</Recipient>
<StatusText>Message has been read</StatusText>
<MM7Version>5.3.0</MM7Version>
</ReadReplyReq>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
我在这里遗漏了最后一步?
附言以及为什么 XPath 不能只是更直观的东西,例如:“//SOAP-ENV:Envelope/SOAP-ENV:Body/ReadReplyReq/MMStatus”,而不是必须跳过所有这些疯狂的箍。
【问题讨论】:
标签: c# .net xml soap linq-to-xml