0

I am trying to deserialize a xml file. I have done this in the past and it always worked like a charm, but not this time. if someone sees my mistake and can help, that would be very much appreciated! The last time I was working with it in unity, this time it is a visual studio console application.

Edit: I probably need to set the "xmlns" attribute somehow, when working in visual studio, but to what?

I get this error message:

 Error in XML-Document (2,2)
<Information xmlns=''> not exspected

My files look like this:

Information.xml:

<?xml version="1.0" encoding="utf-8"?>
<InformationenContainer>
  <Informations>
    <Information ID="foo"/>
  </Informations>
</InformationenContainer>

Program.cs:

class Program
{
    static void Main(string[] args)
    {
        string filename = "C:\\Users\\Ruben Bohnet\\Documents\\Visual Studio 2015\\Projects\\Tool\\Tool\\Informationen.xml";
        InformationenContainer IC = InformationenContainer.Load(filename);
    }
}

InformationController.cs:

using System.IO;
using System.Xml.Serialization;


[XmlRoot("InformationenContainer")]
public class InformationenContainer
{
    [XmlArray("Informations"), XmlArrayItem("Information",typeof(Information))]
    public Information[] Informations;

    public static InformationenContainer Load(string path)
    {
        var serializer = new XmlSerializer(typeof(InformationenContainer));
        using (var stream = new FileStream(path, FileMode.Open))
        {
            InformationenContainer ic = serializer.Deserialize(stream) as InformationenContainer;
            return ic;
        }
    }

}

Information.cs:

using System;
using System.Xml.Serialization;

public class Information
{
    //Attribuites to serialize
    [XmlAttribute("ID")]
    public String ID;
}
Ruben Bohnet
  • 392
  • 1
  • 12
  • 1
    The path to the file you are trying to load does not match the name of the file you've mentioned - is that just a typo or a bug? – jropella May 13 '17 at 03:57
  • Hey, it was just a typo. Thank you for looking though! I found the solution by just creating an object and serializing it. Basically doing it the other way around :) – Ruben Bohnet May 13 '17 at 04:04

1 Answers1

0

Found the solution myself by just creating an InformationContainer and serializing it, the resulting xml file looked like this:

<?xml version="1.0"?>
<InformationenContainer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Informations>
    <Information ID="foo" />
  </Informations>
</InformationenContainer>
Ruben Bohnet
  • 392
  • 1
  • 12
  • 1
    You could also do this: http://stackoverflow.com/questions/870293/can-i-make-xmlserializer-ignore-the-namespace-on-deserialization – mageos May 13 '17 at 04:33