我的班级代码如下,
public class ClassGPS
{
[XmlElement("head")]
public Head head;
[XmlElement("events")]
public Events events;
public class Head
{
[XmlAttribute("nemo")]
public string Nemo { get; set; }
[XmlAttribute("dte")]
public string Dte { get; set; }
}
public class Events
{
[XmlElement("event")]
public _EVENT events;
}
public class _EVENT
{
[XmlAttribute("type")]
public string Type { get; set; }
[XmlAttribute("desc")]
public string Desc { get; set; }
[XmlElement("gps")]
public _GPS gps;
[XmlElement("mov")]
public _MOV mov;
[XmlElement("geo")]
public _GEO geo;
[XmlElement("passengers")]
public _PASSENGERS passengers;
[XmlElement("sensors")]
public _SENSORS sensors;
}
public class _GPS
{
[XmlAttribute("id")]
public string Id { get; set; }
[XmlAttribute("dte")]
public string Dte { get; set; }
}
public class _MOV
{
[XmlAttribute("plate")]
public string Plate { get; set; }
[XmlAttribute("vel")]
public string Vel { get; set; }
[XmlAttribute("odo")]
public string Odo { get; set; }
[XmlAttribute("rpm")]
public string Rpm { get; set; }
[XmlAttribute("rutDri")]
public string Rutdri { get; set; }
[XmlAttribute("tipDri")]
public string Tipdri { get; set; }
}
public class _GEO
{
[XmlAttribute("lat")]
public string Lat { get; set; }
[XmlAttribute("long")]
public string Long { get; set; }
[XmlAttribute("alt")]
public string Alt { get; set; }
[XmlAttribute("dir")]
public string Dir { get; set; }
}
public class _PASSENGERS
{
[XmlElement("pass")]
public _PASS pass;
}
public class _PASS
{
[XmlAttribute("id")]
public string Id { get; set; }
[XmlAttribute("rut")]
public string Rut { get; set; }
[XmlAttribute("tip")]
public string Tip { get; set; }
}
public class _SENSORS
{
[XmlElement("sensor")]
public _SENSOR sensor;
}
public class _SENSOR
{
[XmlAttribute("type")]
public string Type { get; set; }
[XmlAttribute("id")]
public string Id { get; set; }
[XmlAttribute("unit")]
public string Unit { get; set; }
[XmlAttribute("value")]
public string Value { get; set; }
}
}
并使用以下代码填写
public ClassGPS esb()
{
List<ClassGPS._EVENT> listevent = new List<ClassGPS._EVENT>();
List<ClassGPS._PASSENGERS> listpassegers = new List<ClassGPS._PASSENGERS>();
List<ClassGPS._SENSORS> listsensors = new List<ClassGPS._SENSORS>();
ClassGPS gps = new ClassGPS
{
head = new ClassGPS.Head { Nemo = "XYS-XML", Dte = "2012-02-02 15:05:05.456" },
events = new ClassGPS.Events
{
for (int i = 1; i <= 5 ; i++) {
listevent.Add (new ClassGPS._EVENT
{
Type = "PPT",
Desc = "position_time",
gps = new ClassGPS._GPS { Id = i, Dte = "2012-02-02 15:05:05.456" },
mov = new ClassGPS._MOV { Plate = "111456-2", Vel = "98", Odo = "45678987", Rpm = "465489", Rutdri = "13649654", Tipdri = "23133216" },
geo = new ClassGPS._GEO { Lat = "-34.324", Long = "70.366", Alt = "32.506", Dir = "86.32123" },
passengers = new ClassGPS._PASSENGERS
{
for (int x = 1; x <= 9 ; x++) {
listpassegers.Add ( new ClassGPS._PASS {
Id = x,
Rut = "2132132",
Tip = "121325646" })
}
},
sensors = new ClassGPS._SENSORS
{
for (int y = 1; x <= 3 ; x++) {
listsensors.Add ( new ClassGPS._SENSOR {
Type ="LS",
Id = y,
Unit= "%" ,
Value="21" })
}
}
})
}
}
};
return gps;
}
我无法填写代码,因为我不知道如何。 我需要以下结果。
<ClassGPS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<head nemo="XYS-XML" dte="2012-02-02 15:05:05.456" />
<events>
<event type="PPT" desc="position_time">
<gps id="213212315646" dte="2012-02-02 15:05:05.456" />
<mov plate="111456-2" vel="98" odo="45678987" rpm="465489" rutDri="13649654" tipDri="23133216" />
<geo lat="-34.324" long="70.366" alt="32.506" dir="86.32123" />
<passengers>
<pass id="1" rut="2132132" tip="121325646" />
</passengers>
<sensors>
<sensor type="LS" id="1" unit="%" value="21" />
<sensor type="LS" id="1" unit="%" value="21" />
</sensors>
</event>
<event type="PPT" desc="position_time">
<gps id="213212315646" dte="2012-02-02 15:05:05.456" />
<mov plate="111456-2" vel="98" odo="45678987" rpm="465489" rutDri="13649654" tipDri="23133216" />
<geo lat="-34.324" long="70.366" alt="32.506" dir="86.32123" />
<passengers>
<pass id="1" rut="2132132" tip="121325646" />
<pass id="2" rut="2132546132" tip="1213256546" />
</passengers>
<sensors>
<sensor type="LS" id="1" unit="%" value="21" />
</sensors>
</event>
</events>
乘客,传感器和事件可以不止一个。
答案 0 :(得分:0)
我已经稍微修改并缩短了您的代码。
public class Gps
{
[XmlElement("head")]
public HeadData Head { get; set; }
[XmlArray("events")]
[XmlArrayItem("event")]
public List<EventData> Events { get; set; }
public class HeadData
{
[XmlAttribute("nemo")]
public string Nemo { get; set; }
[XmlAttribute("dte")]
public DateTime Dte { get; set; }
}
public class EventData
{
[XmlAttribute("type")]
public string Type { get; set; }
[XmlAttribute("desc")]
public string Desc { get; set; }
[XmlElement("gps")]
public GpsData Gps { get; set; }
}
public class GpsData
{
[XmlAttribute("id")]
public string Id { get; set; }
[XmlAttribute("dte")]
public DateTime Dte { get; set; }
}
}
填充数据并序列化
private static void Main(string[] args)
{
var eventData = new List<Gps.EventData>();
for (int i = 0; i < 5; ++i)
{
eventData.Add(new Gps.EventData
{
Desc = "X",
Type = "Y",
Gps = new Gps.GpsData
{
Dte = DateTime.Now,
Id = i.ToString()
}
});
}
Gps gps = new Gps()
{
Head = new Gps.HeadData {Dte = DateTime.Now, Nemo = "XYS-XML"},
Events = eventData
};
var serializer = new XmlSerializer(gps.GetType());
using (var writer = new StreamWriter(@"C:\temp\gps.xml"))
{
serializer.Serialize(writer, gps);
}
Console.ReadLine();
}
XML结果
<?xml version="1.0" encoding="utf-8"?>
<Gps xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<head nemo="XYS-XML" dte="2015-04-27T18:48:47.9499022+02:00" />
<events>
<event type="Y" desc="X">
<gps id="0" dte="2015-04-27T18:48:47.9489022+02:00" />
</event>
<event type="Y" desc="X">
<gps id="1" dte="2015-04-27T18:48:47.9499022+02:00" />
</event>
<event type="Y" desc="X">
<gps id="2" dte="2015-04-27T18:48:47.9499022+02:00" />
</event>
<event type="Y" desc="X">
<gps id="3" dte="2015-04-27T18:48:47.9499022+02:00" />
</event>
<event type="Y" desc="X">
<gps id="4" dte="2015-04-27T18:48:47.9499022+02:00" />
</event>
</events>
</Gps>