我需要做的只是使用 System.XML 读取在 Packet Name="PacketOut" 中声明的 UnityObject 值。任何帮助将不胜感激,在此先感谢。这是截断的 XML:
<IOConfig>
<Packet Name="PacketOut" RemoteAddress="10.0.2.250" RemotePort="2020" NetworkInterface="ETH0" >
<Signal Type="float" Name="x_out" Unit="m/s" Routing="" UnityObject="CraneBoom" ></Signal>
<Signal Type="float" Name="y_out" Unit="m/s" Routing="" UnityObject="CraneBoom" ></Signal>
<Signal Type="float" Name="z_out" Unit="m/s" Routing="" UnityObject="CraneBoom" ></Signal>
<Signal Type="float" Name="alpha_out" Unit="m/s" Routing="" UnityObject="CraneBoom" ></Signal>
<Signal Type="float" Name="theta_out" Unit="m/s" Routing="" UnityObject="CraneBoom" ></Signal>
<Signal Type="float" Name="phi_out" Unit="m/s" Routing="" UnityObject="CraneBoom" ></Signal>
<Signal Type="float" Name="x_out1" Unit="m/s" Routing="" UnityObject="CraneSlewing" ></Signal>
<Signal Type="float" Name="y_out1" Unit="m/s" Routing="" UnityObject="CraneSlewing" ></Signal>
<Signal Type="float" Name="z_out1" Unit="m/s" Routing="" UnityObject="CraneSlewing" ></Signal>
<Signal Type="float" Name="alpha_out1" Unit="m/s" Routing="" UnityObject="CraneSlewing" ></Signal>
<Signal Type="float" Name="theta_out1" Unit="m/s" Routing="" UnityObject="CraneSlewing" ></Signal>
<Signal Type="float" Name="phi_out1" Unit="m/s" Routing="" UnityObject="CraneSlewing" ></Signal>
<Signal Type="float" Name="x_out2" Unit="m/s" Routing="" UnityObject="CraneBoom" ></Signal>
<Signal Type="float" Name="y_out2" Unit="m/s" Routing="" UnityObject="CraneBoom" ></Signal>
<Signal Type="float" Name="z_out2" Unit="m/s" Routing="" UnityObject="CraneBoom" ></Signal>
<Signal Type="float" Name="alpha_out2" Unit="m/s" Routing="" UnityObject="CraneBoom" ></Signal>
<Signal Type="float" Name="theta_out2" Unit="m/s" Routing="" UnityObject="CraneBoom" ></Signal>
<Signal Type="float" Name="phi_out2" Unit="m/s" Routing="" UnityObject="CraneBoom" ></Signal>
</Packet>
<Packet Name="PacketIn" LocalPort="2021" NetworkInterface="ETH0">
<Signal Type="float" Name="x_in" Unit="m/s" Routing="" UnityObject="CraneBoomCrash"></Signal>
<Signal Type="float" Name="y_in" Unit="m/s" Routing="" UnityObject="CraneBoomCrash"></Signal>
<Signal Type="float" Name="z_in" Unit="m/s" Routing="" UnityObject="CraneBoomCrash"></Signal>
<Signal Type="float" Name="alpha_in" Unit="m/s" Routing="" UnityObject="CraneBoomCrash"></Signal>
<Signal Type="float" Name="theta_in" Unit="m/s" Routing="" UnityObject="CraneBoomCrash"></Signal>
<Signal Type="float" Name="phi_in" Unit="m/s" Routing="" UnityObject="CraneBoomCrash"></Signal>
<Signal Type="float" Name="x_in1" Unit="m/s" Routing="" UnityObject="CraneBoomCrash"></Signal>
<Signal Type="float" Name="y_in1" Unit="m/s" Routing="" UnityObject="CraneBoomCrash"></Signal>
<Signal Type="float" Name="z_in1" Unit="m/s" Routing="" UnityObject="CraneBoomCrash"></Signal>
<Signal Type="float" Name="alpha_in1" Unit="m/s" Routing="" UnityObject="CraneBoomCrash"></Signal>
<Signal Type="float" Name="theta_in1" Unit="m/s" Routing="" UnityObject="CraneBoomCrash"></Signal>
<Signal Type="float" Name="phi_in1" Unit="m/s" Routing="" UnityObject="CraneBoomCrash"></Signal>
</Packet>
</IOConfig>
我尝试了下面的代码,它在 XML 的其余部分中获取了 UnityObject 的所有值。
using UnityEngine;
using System.IO;
using System.Xml;
using System.Text;
using System.Collections.Generic;
using System.Collections;
public class xmlreader : MonoBehaviour
{
public static List<string> values;
public string gameObjects;
public XmlReaderSettings settings;
void Start ()
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
values = new List<string>();
using ( XmlReader reader = XmlReader.Create("D://Docs/Unity3D/cdp/UDPApplication/Components/UDPIOServer.xml", settings) ) {
while ( reader.Read() ) {
if ( reader.NodeType == XmlNodeType.Element) {
if ( reader.HasAttributes ) {
if ( reader.GetAttribute("UnityObject") != null ) {
gameObjects = reader.GetAttribute("UnityObject");
values.Add(gameObjects);
}
}
}
}
}
}
}