3

I have multiple classes of objects and am attempting to convert them to one xml document.

The first class is:

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

The second is:

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

The xml structure I am looking for is

<GAME>
    <EVENTS>
        <event>         
        </event>    
    <EVENTS>

<OBJECTS>
    <object>            
    </object>
<OBJECTS>

4
  • 1
    Can you show your desired xml file structure? Commented Sep 24, 2015 at 16:04
  • Why don't you create a wrapper class and serialize that into xml? Commented Sep 24, 2015 at 16:11
  • <GAME> <EVENTS> <event> <eventID>1</eventID> <eventDescription>Used rock</eventDescription> <hasEventOccured>0</hasEventOccured> </event> <EVENTS> <OBJECTS> <object> <objectID>1</objectID> <objectDescription>A large safe with a number pad</objectDescription> <objectName>SAFE</objectName> <squareID>115</squareID> </object> <OBJECTS> <GAME> Commented Sep 24, 2015 at 16:11
  • @jsomers89 please update the question: XML is even less readable in comments than blocks of code. Commented Sep 24, 2015 at 16:13

3 Answers 3

1

It can be done in a single expression, the parameters of the XElement constructor after its name are used to create children, and collections are expanded so a LINQ expression will create one child for each node (XElement creates a child element, XAttribute adds an attribute).

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
                       )
                    ));
Sign up to request clarification or add additional context in comments.

Comments

0

You can define another class like this:

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

    [DataMember(Name = "EVENTS")]
    List<GameEvent> Events { get; set; }
}

Then using a generic method like this you can instantiated a new Game and pass it this method:

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();
}

If DataContract attribute is not availabe, don't forget to add reference to System.Runtime.Serialization.

Comments

0

Create the structure of your class like below: Create the structure of your class like below and the serialize the object of class by assigning value to its properties:

[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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.