I have started the move from COM to .Net in a solution which involves sending a webservice request and receiving and getting data out of the response. The request and getting the XML data back into an variable of the type System.Xml.XmlDocument.System.Xml is no problem (lets call this variable for "XMLDoc" here). I can also use XMLDoc.GetElementsByTagName('NodeName') to navigate to a set of nodes to loop through. My problem starts when I want to get values from child nodes.
Example of XML:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ReqResponse xmlns="http://services.thedomain.info/the.appname.v3">
<ReqResult>
<Result>Saved</Result>
<Number>6609403768</Number>
<Id>0114</Id>
<Details>
<Detail>
<Number>373400306320689675</Number>
</Detail>
<Detail>
<Number>373400306320689859</Number>
</Detail>
</Details>
</ReqResult>
</ReqResponse>
</soap:Body>
</soap:Envelope>
With COM i could do like this to get the value from the node called 'Result':
CurrNodeList := XMLDoc.getElementsByTagName(ResponseText);
CurrNode := CurrNodeList.item(0);
CurrNode2 := CurrNode.selectSingleNode('Result');
MESSAGE(CurrNode2.text);
This would give a message box saying "Saved" with the XML sample data above. But with .Net variables "SelectSingleNode" does not return anything. I think it has something to do with the namespace which suddenly seems to have become important. I just haven't figured how to deal with it yet so any help would be great. ](*,)
Another common thing I often do with the xml data is to loop through instances of nodes such as the "ReqResult" above. Then I use CurrNode := CurrNodeList.item(0..n) in a FOR loop and want to pick values from the child nodes. This is easy with COM but of course I get the same problem as in my example above.
Any hint in the right direction is appreciated
Edit: I got the solution for this in an email just after I posted this \:D/
The trick is to assign a NamspaceManager variable. The variable is of the type System.Xml.XmlNamespaceManager.System.Xml. Lets call it "NsMgr". When the XMLDoc is loaded these two lines of code is needed to assign NsMgr based on the XML example above:
NsMgr := NsMgr.XmlNamespaceManager(XMLDoc.NameTable);
NsMgr.AddNamespace('ns', 'http://services.thedomain.info/the.appname.v3');
And the node is found with:
CurrNode2 := CurrNode.SelectSingleNode('ns:Result');
//Lars
Answers
You showed the code snippet with COM
Could you show the full code snippet with .NET too?
It was already there in the post, but I have set code tags on it now.
http://www.linkedin.com/in/larswestman
The XML File:
Like it is shown in the first post of this topic:
So, for your example, you will have to define the namespace manager as following: and select the node by using: I hope it will work, I haven't tested it.
http://www.sist.biz/en/ [en] | http://www.sist.biz [de]
Anyway my complete solution below:
What it does is that it will auto add all name spaces.
XML File:
Variables:
Code: