Reputation: 107
All I need to do is just read a values of UnityObject stated in Packet Name="PacketOut" using System.XML. Any help will be appreciated, thanks in advance. Here is XML snipped:
<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>
I tried code snipped below and it is gets all values of UnityObject in the rest of the XML.
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);
}
}
}
}
}
}
}
Upvotes: 2
Views: 711
Reputation: 1917
Do you know xsd.exe
, it's a tool shipped with Visual Studio ?
Note that I renamed your xml file IOConfig.xml
Try the following :
Infer the xsd schema from the file and generate IOConfig.xsd
xsd.exe IOConfig.xml
Generate a nice IOConfig.cs file that might help you in your journey
xsd.exe IOConfig.xsd /classes
Now you just have to deserialize and handle your object :
public void ReadIOConfig()
{
string fileName = @"C:\IOConfig.xml";
XmlSerializer xs = new XmlSerializer(typeof(IOConfig));
XmlReader reader = XmlReader.Create(fileName);
IOConfig config = xs.Deserialize(reader) as IOConfig;
var packetOut = (from configPacket in config.Items
where configPacket.Name == "PacketOut"
select configPacket).First();
foreach (var signal in packetOut.Signal)
Console.WriteLine(signal.UnityObject);
}
using the generated file :
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:4.0.30319.269
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
using System.Xml.Serialization;
//
// This source code was auto-generated by xsd, Version=4.0.30319.1.
//
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class IOConfig
{
private IOConfigPacket[] itemsField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Packet", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public IOConfigPacket[] Items
{
get
{
return this.itemsField;
}
set
{
this.itemsField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class IOConfigPacket
{
private IOConfigPacketSignal[] signalField;
private string nameField;
private string remoteAddressField;
private string remotePortField;
private string networkInterfaceField;
private string localPortField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Signal", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public IOConfigPacketSignal[] Signal
{
get
{
return this.signalField;
}
set
{
this.signalField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string RemoteAddress
{
get
{
return this.remoteAddressField;
}
set
{
this.remoteAddressField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string RemotePort
{
get
{
return this.remotePortField;
}
set
{
this.remotePortField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string NetworkInterface
{
get
{
return this.networkInterfaceField;
}
set
{
this.networkInterfaceField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string LocalPort
{
get
{
return this.localPortField;
}
set
{
this.localPortField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class IOConfigPacketSignal
{
private string typeField;
private string nameField;
private string unitField;
private string routingField;
private string unityObjectField;
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Name
{
get
{
return this.nameField;
}
set
{
this.nameField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Unit
{
get
{
return this.unitField;
}
set
{
this.unitField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string Routing
{
get
{
return this.routingField;
}
set
{
this.routingField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAttributeAttribute()]
public string UnityObject
{
get
{
return this.unityObjectField;
}
set
{
this.unityObjectField = value;
}
}
}
Upvotes: 0
Reputation: 8417
This can be done with a pretty simple XPath expression:
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
is now a List<string>
that looks like {"CraneBoom", "CraneBoom", ...}
.
This solution only uses System.Collections.Generic
and System.Xml
.
Upvotes: 1