Khaine775
Khaine775

Reputation: 2765

Getting information from my XML

I'm quite new to XML serialization/deserialization and I'm wondering how I should apply tags (XmlElement, XmlAttribute etc) to the following objects which I'm writing to XML files, to make it most optimal for me to later use LINQ or similar to get out the data I want. Let me give an example:

    [XmlRoot("Root")]
    public class RootObject
    {
        public Services Services { get; set; }
    }

    public class Services
    {
        public Service TileMapService { get; set; }
    }

    public class Service
    {
        public string Title { get; set; }
        public string href { get; set; }
    }

Here I've defined some properties which I'm going to write to XML with some values I'm gonna add later. At this point, I've hardcoded in the values in this method:

public static void RootCapabilities()
    {
        RootObject ro = new RootObject()
        {
            Services = new Services()
            {
                TileMapService = new Service()
                {
                    Title = "Title",
                    href = "http://something"
                }
            }
        };

Which gets me this XML output:

<?xml version="1.0" encoding="utf-8"?>
<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Services>
    <TileMapService>
      <Title>Title</Title>
      <href>http://something</href>
    </TileMapService>
  </Services>
</Root>

My question is if this is a valid way of doing it or if I have to use the 'XmlElement' and 'XmlAttribute' tags to later deserialize this XML file and get the information out of it, that I want (for example 'title').

I haven't been sure how to write this question, so please let me know if it's too vague.

Upvotes: 0

Views: 49

Answers (3)

gcarvelli
gcarvelli

Reputation: 1580

If you don't want to worry about the nitty-gritty of the serialization, XmlSerializer has always treated me well.

public static void SerializeObject(T obj, string file)
{
    XmlSerializer s = new XmlSerializer(typeof(T));
    TextWriter w = new StreamWriter(file);
    s.Serialize(w, obj);
    w.Close();
}

public static T DeserializeObject(string file)
{
    XmlSerializer s = new XmlSerializer(typeof(T));
    using (StreamReader r = new StreamReader(file))
    {
        return (T)s.Deserialize(r);
    }
}

This should really only be used with references types though (objects, not primitive types or structs). Deserialize can return null and casting that to a value type will bring nothing but heartache.

Upvotes: 1

faby
faby

Reputation: 7558

if the output is what you expect I think that you shouldn't change anything.

You should use Decorators like XmlAttribute or XmlIgnore when you want change the default behavior (e.g don't include a field, include one field as an attribute...)

Their role is obtain full control of the serialization and avoid unexpected behaviors

Upvotes: 1

toadflakz
toadflakz

Reputation: 7944

I would not use XML serialization if I were you. Any changes to your schema/object structure and you immediately lose backwards compatibility due to how XML serialization works as a technology.

Rather separate out the XML serialization from the actual class structure - this way you can make changes to the XML/object schema and write proper migrating functions to handle any changes you make in the future.

Rather use the LINQ-to-XML classes to construct your XML document and save it as they are the easiest way in .NET to convert object structures to XML in a decoupled manner.

Upvotes: 1

Related Questions