2015-04-27 23 views
0

我有我的課的下面的代碼,增值類和子類與C#

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

請說明是什麼問題。 – NathanOliver

+0

您的代碼看起來是使用xml序列化,第二個片段填充所述類實例。你需要使用xmlSerailizer類。 –

+0

@NathanTregillus道歉,但我可以用我的代碼做到這一點。沒有太多關於XML和類 –

回答

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> 
+0

非常感謝你,你在找什麼 –