统一阅读 xml 回复 Android

Read xml response in unity Android

我从 unityWebRequest 得到了下面的响应,休息一下 api。

我想要的是每个对象的名称和值,即在 table 中。 我想将名称用作字符串,将值用作 int。

怎么做??

我看了很多教程,但它们都使用了本地可用的 xmldocument。

请帮助我完成此操作的代码。

提前致谢

    <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>

因为你已经有回复了string:

您可以像这样解析它来创建 XMLElement

XElement response= XElement.Parse(responseString);

您可以这样创建 XMLDocument

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

注意:您的响应字符串必须是有效的 xml 语法。

认为您需要使用 XPath 表达式来提取所需信息。

更新:

这里有一个示例代码,它会给你一个思路:

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);
    }
}

这里有几个入门链接:

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

希望对您有所帮助