我的XML的一部分看起来像这样:
<IOConfig>
<Packet Name="PacketOut" RemoteAddress="10.0.2.223" RemotePort="2020" NetworkInterface="ETH0">
<Signal Type="float" Name="x_out" Unit="m/s" Routing="" UnityObject="CraneBoom" UnityName="Out" NetworkConvert="1"></Signal>
<Signal Type="float" Name="y_out" Unit="m/s" Routing="" UnityObject="CraneBoom" UnityName="Out" NetworkConvert="1"></Signal>
<Signal Type="float" Name="z_out" Unit="m/s" Routing="" UnityObject="CraneBoom" UnityName="Out" NetworkConvert="1"></Signal>
<Signal Type="float" Name="alpha_out" Unit="m/s" Routing="" UnityObject="CraneBoom" UnityName="Out" NetworkConvert="1"></Signal>
<Signal Type="float" Name="theta_out" Unit="m/s" Routing="" UnityObject="CraneBoom" UnityName="Out" NetworkConvert="1"></Signal>
<Signal Type="float" Name="phi_out" Unit="m/s" Routing="" UnityObject="CraneBoom" UnityName="Out" NetworkConvert="1"></Signal>
<Signal Type="float" Name="x_out1" Unit="m/s" Routing="" UnityObject="CraneSlewing" UnityName="Out1" NetworkConvert="1"></Signal>
<Signal Type="float" Name="y_out1" Unit="m/s" Routing="" UnityObject="CraneSlewing" UnityName="Out1" NetworkConvert="1"></Signal>
<Signal Type="float" Name="z_out1" Unit="m/s" Routing="" UnityObject="CraneSlewing" UnityName="Out1" NetworkConvert="1"></Signal>
<Signal Type="float" Name="alpha_out1" Unit="m/s" Routing="" UnityObject="CraneSlewing" UnityName="Out1" NetworkConvert="1"></Signal>
<Signal Type="float" Name="theta_out1" Unit="m/s" Routing="" UnityObject="CraneSlewing" UnityName="Out1" NetworkConvert="1"></Signal>
<Signal Type="float" Name="phi_out1" Unit="m/s" Routing="" UnityObject="CraneSlewing" UnityName="Out1" NetworkConvert="1"></Signal>
</Packet>
<Packet Name="PacketIn" LocalPort="2021" NetworkInterface="ETH0">
<Signal Type="float" Name="x_in" Unit="m/s" Routing="" UnityObject="CraneBoomCrash" UnityName="collider1" NetworkConvert="1"></Signal>
<Signal Type="float" Name="y_in" Unit="m/s" Routing="" UnityObject="CraneBoomCrash" UnityName="collider1" NetworkConvert="1"></Signal>
<Signal Type="float" Name="z_in" Unit="m/s" Routing="" UnityObject="CraneBoomCrash" UnityName="collider1" NetworkConvert="1"></Signal>
<Signal Type="float" Name="alpha_in" Unit="m/s" Routing="" UnityObject="CraneBoomCrash" UnityName="collider1" NetworkConvert="1"></Signal>
<Signal Type="float" Name="theta_in" Unit="m/s" Routing="" UnityObject="CraneBoomCrash" UnityName="collider1" NetworkConvert="1"></Signal>
<Signal Type="float" Name="phi_in" Unit="m/s" Routing="" UnityObject="CraneBoomCrash" UnityName="collider1" NetworkConvert="1"></Signal>
</Packet>
</IOConfig>
所需的输出为:Out,Out1,collider1。
也就是说,不应打印UnityName的重复值。可以这样做吗?
代码剪断了读取UnityName的重复值:
public class XMLStringReader : MonoBehaviour
{
public string slectedItem;
private bool editing = false;
public string signalsa;
public XmlReaderSettings settings;
void OnGUI ()
{
if ( GUILayout.Button(slectedItem))
{
editing = true;
}
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.IgnoreComments = true;
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("UnityName") != null )
{
signalsa = reader.GetAttribute("UnityName");
string [] signalsCDP = {signalsa};
if (editing)
{
for(int i =0; i < signalsCDP.Length; i++)
{
if (GUILayout.Button(signalsCDP[i]))
{
slectedItem = signalsCDP[i];
editing = false;
print(signalsCDP[i].ToString());
}
}
}
}
}
}
}
}
}
}
答案 0 :(得分:3)
您可以使用LINQ to XML完成任务:
XElement x = XElement.Load("File containing your xml");
var list = x.Descendants("Signal")
.Where(d => d.HasAttributes && d.Attribute("UnityName") != null)
.Select(d => d.Attribute("UnityName").Value)
.Distinct();
foreach (string s in list)
Console.WriteLine(s);
答案 1 :(得分:1)
迭代所有值,并使用包含检查将它们添加到List / Dictionary / Array / Whatever。之后打印收集的值。
以下是解决方案:
internal static void PrintAttributeValues(XDocument scr)
{
List<string> values = new List<string>();
foreach (XElement elem in scr.Descendants("Signal"))
{
if (!values.Contains(elem.Attribute("UnityName").Value))
{
values.Add(elem.Attribute("UnityName").Value);
}
}
for (int j = 0; j < values.Count; j++)
{
Console.WriteLine(values[j]);
}
}
答案 2 :(得分:0)
另一种方法是为程序化方法之外的任何XML定义 XML Schema 。 XML Schema 确保XML的合规性。
XML Schema描述了XML文档的结构。
查看以下参考资料:
答案 3 :(得分:0)
只需创建一个包含不同值的集合。然后检查下一个属性值是否已在该集合中。如果没有,请将其添加到集合中并执行您需要的任何操作。
List<string> values = new List<string>();
using (XmlReader reader = XmlReader.Create("your path", settings))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.HasAttributes)
{
if (reader.GetAttribute("UnityName") != null)
{
signalsa = reader.GetAttribute("UnityName");
if (!values.Contains(signalsa))
{
values.Add(signalsa);
//rest of your code goes here...
}
}
}
}
}
}