生成带有多个对象的XML

本文关键字:对象 XML | 更新日期: 2023-09-27 18:14:20

我有多个对象类,并试图将它们转换为一个xml文档。

第一类是:

public class GameObject
{
// data members
public int squareID;
public int objectID;
public String objectDescription;
public String objectName;
}

第二个是:

public class GameEvent
{
// data members
public int eventID;
public String eventDescription;
public int hasEventOccured;
}

我要找的xml结构是

<GAME>
    <EVENTS>
        <event>         
        </event>    
    <EVENTS>
<OBJECTS>
    <object>            
    </object>
<OBJECTS>

生成带有多个对象的XML

这可以在单个表达式中完成,XElement构造函数的名称后的参数用于创建子节点,并且集合被扩展,因此LINQ表达式将为每个节点创建一个子元素(XElement创建子元素,XAttribute添加属性)。

var content = new XElement("GAME",
                    new XElement("EVENTS",
                       from e in AllEvents
                       select new XElement("EVENT",
                              new XELement("eventID", e.eventId),
                              new XElement("eventDescription", e.eventDescription),
                              new XElement("hasEventOccured", e.hasEventOccured)
                       )
                    ),
                    new XElement("OBJECTS",
                       from obj in AllObjects
                       select new XElement("OBJECT",
                             // make content for a single object
                       )
                    ));

您可以像这样定义另一个类:

[DataContract(Name ="GAME")]
public class Game
{
    [DataMember(Name = "OBJECTS")]
    List<GameObject> Objects { get; set; }
    [DataMember(Name = "EVENTS")]
    List<GameEvent> Events { get; set; }
}

然后使用像这样的泛型方法,您可以实例化一个新的Game并将此方法传递给它:

public static void Serialize<T>(string path, T value)
{
    System.Xml.Serialization.XmlSerializer serializer = 
                 new System.Xml.Serialization.XmlSerializer(typeof(T));
    System.Xml.XmlTextWriter writer = 
                 new System.Xml.XmlTextWriter(path, System.Text.Encoding.UTF8);
    serializer.Serialize(writer, value);
    writer.Close();
}

如果DataContract属性不可用,不要忘记添加对System.Runtime.Serialization的引用

创建你的类的结构如下:如下所示创建类的结构,并通过为其属性赋值来序列化类的对象:

[XmlRoot("GAME")]
    public class Game
    {
        [XmlElement("EVENTS")]
        public Events Events { get; set; }
        [XmlElement("OBJECTS")]
        public GameObject GameObject { get; set; }
    }

    public class Events
    {
        [XmlElement("EVENTS")]
        public GameEvent Event;
    }
    public class Object
    {
        [XmlElement("object")]
        public GameObject GameObject;
    }
    public class GameEvent
    {
        [XmlElement("eventID")]
        public int eventID;
        [XmlElement("eventDescription")]
        public String eventDescription;
        [XmlElement("hasEventOccured")]
        public int hasEventOccured;
    }
    public class GameObject
    {
        [XmlElement("squareID")]
        public int squareID;
        [XmlElement("objectID")]
        public int objectID;
        [XmlElement("objectDescription")]
        public String objectDescription;
        [XmlElement("objectName")]
        public String objectName;
    }