Search code examples
androidunity-game-enginexmlhttprequestwebrequest

Read xml response in unity Android


I got this below response from a unityWebRequest for a rest api.

What i want is that name and value of each of the object i.e inside a table. I want to use the name as string and value as int.

How to do it ??

I saw many tutorials but all of them make use of a locally available xmldocument.

Please help me the code to do this.

Thanks in advance

    <HEAD>
    <TITLE>Property Listing For SimulationData</TITLE>
    <LINK rel='Stylesheet' href='/Thingworx/css/thingworxapi.css' type='text/css'></LINK>
    <META http-equiv='Content-Type' content='text/html'></META>
    <META http-equiv='cache-control' content='no-cache, no-store'></META>
    <META http-equiv='expires' content='-1'></META>
    <META http-equiv='pragma' content='no-cache, no-store'></META>
    <META http-equiv='refresh' content='30'></META>
</HEAD>
<BODY>
    <IMG SRC="/Thingworx/images/ThingworxLogo.png"/>
    <BR/>
    <H1>Property Listing For SimulationData</H1>
    <TABLE>
        <TR>
            <TH>name</TH>
            <TH>value</TH>
        </TR>
        <TR>
            <TD>ActualSpeed</TD>
            <TD>0</TD>
        </TR>
        <TR>
            <TD>AirPressure</TD>
            <TD>8.0</TD>
        </TR>
        <TR>
            <TD>Capacity</TD>
            <TD>1500.0</TD>
        </TR>
        <TR>
            <TD>Conveyor_Speed</TD>
            <TD>75.0</TD>
        </TR>
        <TR>
            <TD>CurrentTemperature</TD>
            <TD>0</TD>
        </TR>
        <TR>
            <TD>description</TD>
            <TD></TD>
        </TR>
        <TR>
            <TD>GetEquipment</TD>
            <TD>
                <TABLE>
                    <TR>
                        <TH>Machine_Name</TH>
                    </TR>
                </TABLE>
            </TD>
        </TR>
        <TR>
            <TD>IdealSpeed</TD>
            <TD>1</TD>
        </TR>
        <TR>
            <TD>isConnected</TD>
            <TD>true</TD>
        </TR>
        <TR>
            <TD>lastConnection</TD>
            <TD>2016-09-15T15&#x3a;16&#x3a;32.111&#x2b;05&#x3a;30</TD>
        </TR>
        <TR>
            <TD>lastConnectionError</TD>
            <TD></TD>
        </TR>
        <TR>
            <TD>LinearSpeed</TD>
            <TD>3.5</TD>
        </TR>
        <TR>
            <TD>Loadweight</TD>
            <TD>0.0</TD>
        </TR>
        <TR>
            <TD>MilkBikiLowerRange</TD>
            <TD>98.0</TD>
        </TR>
        <TR>
            <TD>MilkBikiUpperRange</TD>
            <TD>108.0</TD>
        </TR>
        <TR>
            <TD>name</TD>
            <TD>SimulationData</TD>
        </TR>
        <TR>
            <TD>NiceLowerRange</TD>
            <TD>91.0</TD>
        </TR>
        <TR>
            <TD>NiceUpperRange</TD>
            <TD>99.0</TD>
        </TR>
        <TR>
            <TD>NoOfRotationAgitator</TD>
            <TD>0.0</TD>
        </TR>
        <TR>
            <TD>NoofRotationConveyor</TD>
            <TD>0.0</TD>
        </TR>
        <TR>
            <TD>NoOfRotationsRotaryMould</TD>
            <TD>30.0</TD>
        </TR>
        <TR>
            <TD>NorthEast</TD>
            <TD>34.2646815,85.7826173,0.0</TD>
        </TR>
        <TR>
            <TD>NorthWest</TD>
            <TD>33.5872439,56.2797477,0.0</TD>
        </TR>
        <TR>
            <TD>OvenTemperature</TD>
            <TD>50.0</TD>
        </TR>
        <TR>
            <TD>PickPlace</TD>
            <TD>50.0</TD>
        </TR>
        <TR>
            <TD>Power</TD>
            <TD>24.0</TD>
        </TR>
        <TR>
            <TD>ScrapCount</TD>
            <TD>0</TD>
        </TR>
        <TR>
            <TD>SouthEast</TD>
            <TD>12.9036622,92.4436689,0.0</TD>
        </TR>
        <TR>
            <TD>SouthWest</TD>
            <TD>39.3923528,29.8171935,0.0</TD>
        </TR>
        <TR>
            <TD>tags</TD>
            <TD>Britania_POC&#x3a;Biscuit_POC&#x3b;FAndB_DemoKit&#x3a;F&amp;B_DemoKit</TD>
        </TR>
        <TR>
            <TD>thingTemplate</TD>
            <TD>MSSQL</TD>
        </TR>
        <TR>
            <TD>TigerLowerRange</TD>
            <TD>100.0</TD>
        </TR>
        <TR>
            <TD>TigerUpperRange</TD>
            <TD>92.0</TD>
        </TR>
        <TR>
            <TD>TotalWorkingTime</TD>
            <TD>300</TD>
        </TR>
        <TR>
            <TD>VibrationPresence</TD>
            <TD>No Vibration</TD>
        </TR>
    </TABLE>
</BODY>


Solution

  • As you already have response string:

    you can create XMLElement by parsing it like this:

    XElement response= XElement.Parse(responseString);
    

    OR

    You can create XMLDocument like this:

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(responseString);
    

    NOTE: your response string has to be a valid xml syntax.

    Thought you need to use XPath expressions to extract required information.

    UPDATE:

    here is a sample code, it will give you an idea:

    void Start()
    {
        string yourActualResponse = "your actual response goes here";
        StringBuilder responseString = new StringBuilder( @"<?xml version='1.0'?>");
        responseString.AppendLine(@"<reponse>");
        responseString.AppendLine(yourActualResponse);
        responseString.AppendLine(@"</reponse>");
    
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(responseString.ToString());
    
        XmlNodeList nodes = doc.DocumentElement.SelectNodes("//TR/TD");
        foreach (XmlNode node in nodes)
        {
            Debug.Log(node.InnerText);
        }
    }
    

    here are few links to get started:

    https://msdn.microsoft.com/en-us/library/cc189056(VS.95).aspx

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

    https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx

    Hope this helps