HomeGenie Forum

Automation Program Plugins and Wizard Scripting => Help => Topic started by: Jens on June 26, 2016, 08:48:39 PM

Title: Parse XML response
Post by: Jens on June 26, 2016, 08:48:39 PM
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
 
Title: Re: Parse XML response
Post by: [email protected] on June 26, 2016, 10:59:01 PM
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?
Title: Re: Parse XML response
Post by: [email protected] on June 26, 2016, 11:10:37 PM

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 (https://msdn.microsoft.com/en-us/library/d271ytdx(v=vs.110).aspx)
Title: Re: Parse XML response
Post by: Jens on June 27, 2016, 08:53:31 PM
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
Title: Re: Parse XML response
Post by: [email protected] on June 28, 2016, 04:22:41 PM
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 :)
Title: Re: Parse XML response
Post by: Jens on June 28, 2016, 09:52:36 PM
Many thanks, this helped me a lot

Regards
Jens
Title: Re: Parse XML response
Post by: [email protected] on June 29, 2016, 10:40:29 AM
Not the best code I've ever written as I hate XML with a passion, but if it works :)
Title: Re: Parse XML response
Post by: Jens on September 28, 2016, 10:22:35 PM
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
Title: Re: Parse XML response
Post by: [email protected] on October 04, 2016, 09:59:02 AM
Try going into the program and re-compile it..
Title: Re: Parse XML response
Post by: Jens on October 04, 2016, 07:53:42 PM
Did that and the error message pops up.

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