gpt4 book ai didi

c# - 如何反序列化 XML 中特定元素中属性的特定值?

转载 作者:太空宇宙 更新时间:2023-11-03 13:51:52 26 4
gpt4 key购买 nike

我需要做的只是使用 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);



}
}
}


}
}



}
}

最佳答案

这可以用一个非常简单的 XPath 来完成表达式:

XmlDocument doc = new XmlDocument();
doc.Load("D://Docs/Unity3D/cdp/UDPApplication/Components/UDPIOServer.xml");

XmlNodeList nodes =
doc.SelectNodes("IOConfig/Packet[@Name='PacketOut']/Signal/@UnityObject");

List<string> result = new List<string>();
foreach (XmlNode node in nodes)
{
result.Add(node.Value);
}

result现在是 List<string>看起来像 {"CraneBoom", "CraneBoom", ...} .

此解决方案仅使用 System.Collections.GenericSystem.Xml .

关于c# - 如何反序列化 XML 中特定元素中属性的特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13475206/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com