0

Edited:

I'm trying to accomplish three things here: get XmlNode Query, get the XmlNode QueryId and get the value of a:schemaLocation but the after parsing they end up as null. If I remove the qualified name from the XML, the C# bit works fine. How should I re-write my code?

XML:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:loc="localhost">
   <soapenv:Header/>
   <soapenv:Body>
      <loc:Service>
         <!--Optional:-->
         <loc:input>?</loc:input>
         <Query a:schemaLocation="http://www.xyz.com/xml.xsd" xmlns:payload="http://www.xyz.com" xmlns:a="http://www.w3.org/2001/XMLSchema-instance" xmlns="loc4">
            <QueryId>Data</QueryId>
         </Query>
      </loc:Service>
   </soapenv:Body>
</soapenv:Envelope>

C#:

private NamespaceManager nsmgr;
private XmlDocument doc = new XmlDocument();
private Stream receiveStream = new Stream(HttpContext.Current.Request.InputStream);
private XmlNode Query, QueryId;

using (this.receiveStream){

using (StreamReader readStream = new StreamReader(this.receiveStream, Encoding.UTF8)){

     doc.Load(readStream);

     this.nsmgr = new XmlNamespaceManager(this.doc.NameTable);
     this.nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
     this.nsmgr.AddNamespace("loc", "http://localhost");
     this.nsmgr.AddNamespace("schemaLocation", "http://www.xyz.com/xml.xsd");
     this.nsmgr.AddNamespace("payload", "http://www.xyz.com");
     this.nsmgr.AddNamespace("a", "http://www.w3.org/2001/XMLSchema-instance");
     this.nsmgr.AddNamespace("x", this.doc.DocumentElement.NamespaceURI);
     this.Query = doc.SelectSingleNode("//x:Query", this.nsmgr);
     this.QueryId= doc.SelectSingleNode("//x:QueryId", this.nsmgr);
     }
 }
tomtomssi
  • 1,017
  • 5
  • 20
  • 33
  • The XML document contains all the necessary information when received but the code above doesn't parse the values "Query" and "QueryId" to their respected XmlNodes. – tomtomssi Aug 07 '13 at 10:58

2 Answers2

1

Here you go...

XmlDocument xDoc = new XmlDocument();
xDoc.Load("Query.xml");

XmlNamespaceManager xnm = new XmlNamespaceManager(xDoc.NameTable);
xnm.AddNamespace("schemaLocation", "loc");
xnm.AddNamespace("payload", "loc2");
xnm.AddNamespace("a", "http://www.w3.org/2001/XMLSchema-instance");
xnm.AddNamespace("x", xDoc.DocumentElement.NamespaceURI);

Inner Text For Query:

xDoc.SelectNodes("//x:Query", xnm)[0].InnerText

Inner Text For QueryId:

xDoc.SelectNodes("//x:QueryId", xnm)[0].InnerText

a:schemaLocation Attribute:

string namespaceURI = xnm.GetNamespacesInScope(XmlNamespaceScope.Local).FirstOrDefault(el => string.Equals(el.Key, "a")).Value;
var x = xDoc.DocumentElement.Attributes["schemaLocation", namespaceURI].Value;
Prash
  • 1,122
  • 1
  • 8
  • 10
  • The nodes are still empty. I modified my post to just grab the nodes. Any idea what's still going wrong? – tomtomssi Aug 07 '13 at 07:04
  • @tomtomssi - I tried this code and could see it fetching results. Can you post the code (from your code file) using which you are trying to extract InnerText and attribute values? – Prash Aug 07 '13 at 08:02
  • I modified the code from the previous example. I removed the InnerText bits because the problem lies with the nodes. I'm using the code above to try to extract the nodes "Query" and "QueryId" but they end up empty. – tomtomssi Aug 07 '13 at 10:25
  • Modified the XML, too – tomtomssi Aug 07 '13 at 10:32
  • Also you need to add to the namespace table, the namespaces _soapenv_ and _loc_ – Prash Aug 07 '13 at 14:41
  • Made the changes but its still empty. Any clue as to why? – tomtomssi Aug 08 '13 at 06:15
0

Here's a simple solution I came up with:

Removed the following lines:

 this.nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
     this.nsmgr.AddNamespace("loc", "http://localhost");
     this.nsmgr.AddNamespace("schemaLocation", "http://www.xyz.com/xml.xsd");
     this.nsmgr.AddNamespace("payload", "http://www.xyz.com");

Modified:

this.nsmgr.AddNamespace("x", "loc4");

Finding the value of a:schemaLocation worked as suggested by Prash.

tomtomssi
  • 1,017
  • 5
  • 20
  • 33