more G-Labs products

Author Topic: Parse XML response  (Read 1688 times)

June 26, 2016, 08:48:39 PM
Read 1688 times

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
Hello,

I am doing a soap request with Net.WebService(...) and .Post("<?xml version=\"1.0\"....

string result = request.Call();

result contains the XML response, doing a

Program.Notify("Show Result", result);

the pop up shows up displaying the values which are part of the xml like

"param1 param2 param3 param4"

If I do a

string[] reqs = (result.Split(' '));

and display reqs [1] I get part of the xml code not param1.

How can I decode the xml?

Many thanks

Best regards
Jens
 

June 26, 2016, 10:59:01 PM
Reply #1

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
don't store the response as a string, store it as XML you can then use Xpath to get what you want, Have you got sample XML you can post?

June 26, 2016, 11:10:37 PM
Reply #2

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271

so you can do something like this and then look at the msdn example and dependant on your xml :)

var doc = new System.Xml.XmlDocument();
doc.LoadXml(request.Call());


have a look at https://msdn.microsoft.com/en-us/library/d271ytdx(v=vs.110).aspx

June 27, 2016, 08:53:31 PM
Reply #3

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
Thanks a lot for helping, this is the xml reply

<?xml version="1.0"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<s:Body>
<u:GetGenericHostEntryResponse xmlns:u="urn:dslforum-org:service:Hosts:1">
<NewIPAddress>192.168.0.1</NewIPAddress>
<NewAddressSource>DHCP</NewAddressSource>
<NewLeaseTimeRemaining>0</NewLeaseTimeRemaining>
<NewMACAddress>AA:BB:CC:DD:EE:FF</NewMACAddress>
<NewInterfaceType>Ethernet</NewInterfaceType>
<NewActive>0</NewActive>
<NewHostName>phone</NewHostName>
</u:GetGenericHostEntryResponse>
</s:Body>
</s:Envelope>

I wanna further process the data of <NewActive>

Again, many thanks

June 28, 2016, 04:22:41 PM
Reply #4

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
Code: [Select]
            var theXmlString = request.Call();

            // Create XML Document and load the XML
            var doc = new System.Xml.XmlDocument();
            doc.LoadXml(theXmlString);
           
            // Create Namespace manager
            var xmlnsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);
            xmlnsManager.AddNamespace("u", "urn:dslforum-org:service:Hosts:1");
            var node = doc.SelectSingleNode("//NewActive", xmlnsManager);

            var newActive = node.InnerText;

            Console.WriteLine(newActive);


Try that, my testing within visual studio suggests it should work :)

June 28, 2016, 09:52:36 PM
Reply #5

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
Many thanks, this helped me a lot

Regards
Jens

June 29, 2016, 10:40:29 AM
Reply #6

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
Not the best code I've ever written as I hate XML with a passion, but if it works :)

September 28, 2016, 10:22:35 PM
Reply #7

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
Hi David

I did an upgrade to mono and I am running now

mono --version
Mono JIT compiler version 4.6.0 (Stable 4.6.0.245/746756c Wed Sep 21 15:11:18 UTC 2016)

the same code causes now the following compile error
The type or namespace name `Xml' does not exist in the namespace `System'. Are you missing `System.Xml' assembly reference?

Do you have any clue what I can do to make it work again?

Thanks

Best Regards
Jens

October 04, 2016, 09:59:02 AM
Reply #8

[email protected]

  • *****
  • Information
  • Hero Member
  • Posts: 271
Try going into the program and re-compile it..

October 04, 2016, 07:53:42 PM
Reply #9

Jens

  • *****
  • Information
  • Global Moderator
  • Posts: 211
Did that and the error message pops up.

As I found no other solution I am meanwhile back on mono 4.2.2.