c# - struggling with XML namespaces (linq)... what am I doing wrong here? -


Something trying to parse XML, but apparently it is too much for a lazy Sunday afternoon, / P>

This is my code: (I tried the XPathDocument and XmlDocument approach, but it also failed too badly)

  XmlDocument xmlDoc = New XmlDocument (); XmlDoc.LoadXml (POSTDATA); XDocument xDoc = XDocument.Load (new XmlNodeReader (xmlDoc)); X-namespace soap = "http://schemas.xmlsoap.org/soap/envelope/"; XElement xEnv = xDoc.Descendants (SOAPON + "envelope"). first (); Excellence xBody = xEnv.Descendants (SOAPOVAN + "BODY"). first (); XReadReply = xBody.Descendants ("ReadReplyReq"). first ();  

The last line fails with the exception: there is no element in this collection, however if I change this last line to:

  XElement xReadReply = xBody.Descendants) .First ();  

This gives the first node which is actually the "Ridleyplayerk" node.

Finally, these namespaces are working, it now fails on the first node without a breed ... oh bitter irony; ^)

This is the XML I am trying to parse:

  & lt; SOAP-ENV: envelope xmlns: SOAP-ENV = "http://schemas.xmlsoap.org/soap/envelope/"> & Lt; Soap-ENV: Header & gt; & Lt; 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" & gt; 12345678 & lt; / TransactionId> & Lt; / SOAP-ENV: Header & gt; & Lt; Soap-ENV: Body & gt; & Lt; ReadReplyReq xmlns = "http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2" & gt; & Lt; MMStatus & gt; Read & lt; / MMStatus & gt; & Lt; Timestamp & gt; 2007-12-13T14: 05: 27 + 01: 00 & lt; / Timestamp & gt; & Lt; MessageId & gt; 54321 & lt; / MessageId> & Lt; Sender & gt; & Lt; Shortcode & gt; + 12345 & lt; / Shortcode & gt; & Lt; / Sender & gt; & Lt; Recipients & gt; & Lt; Number & gt; + 12345 & lt; / Number & gt; & Lt; / Recipient & gt; & Lt; Statistic & gt; Message read & lt; / StatusText> & Lt; MM7Version & gt; 5.3.0 & lt; / MM7Version> & Lt; / ReadReplyReq & gt; & Lt; / SOAP-ENV: Body & gt; & Lt; / SOAP-ENV: envelope & gt;  

What am I missing here?

And why not XPath just be some more intuitive: "// SOAP-ENV: envelope / soap-ENV: body / readley reek / mmstatus", one of these all instead of crazy hoops To jump through.

I think what you want is to add a namespace manager so that you select XPath on the pre-paid node Can you?

If so, something like this:

  var doc = new XmlDocument (); Doc.LoadXml (PostData); Var ns = New XmlNamespaceManager (doc.NameTable); Ns.AddNamespace ("SOAP-ENV", "http://schemas.xmlsoap.org/soaf/envelope/"); Ns.AddNamespace ("def", "http://www.3gpp.org/ftp/Specs/archive/23_series/23.140/schema/REL-5-MM7-1-2"); XmlNode list = doc.SelectSingleNode ("// SOAP-ENV: envelope / SOEP-ENV: body / DIF: readley repreive / DIF: MMStatus", ns);  

Maybe what you want


Comments

Popular posts from this blog

asp.net - Javascript/DOM Why is does my form not support submit()? -

sockets - Delphi: TTcpServer, connection reset when reading -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -