Khaine775
Khaine775

Reputation: 2765

Error when deserializing

I'm trying to write some code to deserialize an XML file. I've looked around a bit and found something which has led me to the following method:

    public static void Deserialize(string filePath)
    {
        RootObject ro = null;
        string path = filePath;

        XmlSerializer serializer = new XmlSerializer(typeof(RootObject));

        StreamReader reader = new StreamReader(path);
        ro = (RootObject) serializer.Deserialize(reader);
        reader.Close();
    }

But all I get is this error and I'm not sure what's causing it:

There is an error in XML document (2, 2).

The RootObject you see in Deserialize() is this one: I'm new to XMl serializing/deserializing so I'm not sure if I've defined it 100% correctly.

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

I'm using this method to create the XML files in the first place and it seems to work fine:

    public static void WriteToXmlFile<T>(string filePath, T objectToWrite) where T : new()
    {
        TextWriter writer = null;
        try
        {
            var serializer = new XmlSerializer(typeof (T));
            writer = new StreamWriter(filePath);
            serializer.Serialize(writer, objectToWrite);
        }
        finally
        {
            if (writer != null)
            {
                writer.Close();
            }
        }
    }

It gets me an XML file that looks like this:

<RootObject xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Services>
    <TileMapService>
      <Title>Some title</Title>
      <href>http://something</href>
    </TileMapService>
  </Services>
</RootObject>

Upvotes: 1

Views: 95

Answers (1)

Marc Gravell
Marc Gravell

Reputation: 1063874

Your code works fine. The "There is an error in XML document (2, 2)." might be because your actual file (not the one created by WriteToXmlFile<T>) has whitespace at the start, or is using the wrong namespace. Check the .InnerException for more detail, or (perhaps simpler) please post the actual xml file contents. Example of it working fine (along with some recommended tweaks to the two key methods):

static void Main()
{
    RootObject obj = new RootObject
    {
        Services = new Services
        {
            TileMapService = new Service
            {
                Title = "abc",
                href = "def"
            }
        }
    };
    WriteToXmlFile("foo.xml", obj);
    var loaded = Deserialize<RootObject>("foo.xml");
    var svc = loaded.Services.TileMapService;
    System.Console.WriteLine(svc.Title); // abc
    System.Console.WriteLine(svc.href); // def
}
public static void WriteToXmlFile<T>(string filePath, T objectToWrite)
{       
    var serializer = new XmlSerializer(typeof(T));
    using (var writer = new StreamWriter(filePath))
    {
        serializer.Serialize(writer, objectToWrite);
    }
}
public static T Deserialize<T>(string filePath)
{
    var serializer = new XmlSerializer(typeof(T));
    using (var reader = new StreamReader(filePath))
    {
        return (T)serializer.Deserialize(reader);
    }
}

Upvotes: 2

Related Questions