Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用c为类和子类添加值#_C#_Xml_Class - Fatal编程技术网

C# 用c为类和子类添加值#

C# 用c为类和子类添加值#,c#,xml,class,C#,Xml,Class,我有我班的以下代码 public class ClassGPS { [XmlElement("head")] public Head head; [XmlElement("events")] public Events events; public class Head { [XmlAttribute("nemo")] public string Nemo { get; set; } [XmlAt

我有我班的以下代码

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;
    }
公共类GPS esb() { List listevent=新列表(); List listpassegers=新列表(); List listsensors=新列表(); ClassGPS=新的ClassGPS { head=newclassgps.head{Nemo=“XYS-XML”,Dte=“2012-02-02 15:05:05.456”, events=newclassgps.events {
对于(inti=1;i我已经稍微修改并缩短了您的代码

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; }
    }
}
公共级Gps
{
[XmlElement(“head”)]
公共头数据头{get;set;}
[XmlArray(“事件”)]
[XmlArrayItem(“事件”)]
公共列表事件{get;set;}
公共类标题数据
{
[XmlAttribute(“nemo”)]
公共字符串Nemo{get;set;}
[XmlAttribute(“dte”)]
公共日期时间Dte{get;set;}
}
公共类事件数据
{
[XmlAttribute(“类型”)]
公共字符串类型{get;set;}
[XmlAttribute(“desc”)]
公共字符串Desc{get;set;}
[XmlElement(“gps”)]
公共GpsData Gps{get;set;}
}
公共类GpsData
{
[XmlAttribute(“id”)]
公共字符串Id{get;set;}
[XmlAttribute(“dte”)]
公共日期时间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();
}
private static void Main(字符串[]args)
{
var eventData=新列表();
对于(int i=0;i<5;++i)
{
添加(新的Gps.eventData
{
Desc=“X”,
Type=“Y”,
Gps=新的Gps.GpsData
{
Dte=日期时间。现在,
Id=i.ToString()
}
});
}
Gps=新Gps()
{
Head=new Gps.HeadData{Dte=DateTime.Now,Nemo=“XYS-XML”},
事件=事件数据
};
var serializer=新的XmlSerializer(gps.GetType());
使用(var writer=newstreamwriter(@“C:\temp\gps.xml”))
{
序列化器。序列化(编写器、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>


请描述问题所在。您的代码似乎在使用xml序列化,第二个代码段填充了所述类实例。您需要使用xmlSerailizer类。@NathanTregillus抱歉,但我可以用我的代码做到这一点。关于xml和classes@NathanOliver问题是,我需要将多个事件插入到列出一个for循环,不要这样做,这不是结果
<?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>