2

I have an XML document provided to me externally that I need to import into my application. The document is badly written, but not something I can really do much about.

<?xml version="1.0" encoding="iso-8859-1"?>
<xml>
    <Items>
        <Property1 />
        <Property2 />
        ...
    </Items>
    <Items>
        <Property1 />
        <Property2 />
        ...
    </Items>
    ...
</xml>

How should I go about using the XmlSerializer for this? It doesn't seem to matter what class setup I use, or wether I put XmlRoot(ElementName="xml") on my base class it always says that the <xml xmlns=''> is unexpected.

Edit: Added the C# code I am using

[XmlRoot(ElementName = "xml")]
public class Container
{
    public List<Items> Items { get; set; }
}

public class Items
{
    public short S1 { get; set; }
    public short S2 { get; set; }
    public short S3 { get; set; }
    public short S4 { get; set; }
    public short S5 { get; set; }
    public short S6 { get; set; }
    public short S7 { get; set; }
}

public class XMLImport
{
    public Container Data{ get; private set; }

    public static XMLImport DeSerializeFromFile(string fileName)
    {
        XMLImport import = new XMLImport();
        XmlSerializer serializer = new XmlSerializer(typeof(Container));
        using (StreamReader reader = new StreamReader(fileName))
            import.Data = (Container)serializer.Deserialize(reader);
        return import;
    }
}
Perry
  • 2,250
  • 2
  • 19
  • 24
  • may be add a node `` under `` and add all the `` in this node. since you are serializing it, hopefully it would be you who has to de-serialize it. – Tauseef Oct 08 '13 at 06:27
  • I mean you can then change the XML back to real (bad) structure after de-serializing if you really need it to be in this form. – Tauseef Oct 08 '13 at 06:29
  • I am getting this XML provided to me to deserialize it and add the data into my app. I am not serializing my data into XML. I prefer to not change the document at all prior to deserializing it. – Perry Oct 08 '13 at 06:30
  • Can you share your C# code? – Tauseef Oct 08 '13 at 06:34

2 Answers2

5

Say you have a class Items maps to each <Items> node:

public class Items
{
    public string Property1 { get; set; }
    public string Property2 { get; set; }
}

You can deserialize your list of Items like this:

var doc = XDocument.Parse(
    @"<?xml version=""1.0"" encoding=""iso-8859-1""?>
    <xml>
        <Items>
            <Property1 />
            <Property2 />
        </Items>
        <Items>
            <Property1 />
            <Property2 />
        </Items>
    </xml>");
var serializer = new XmlSerializer(typeof(List<Items>), new XmlRootAttribute("xml"));
List<Items> list = (List<Items>)serializer.Deserialize(doc.CreateReader());
Fung
  • 3,508
  • 2
  • 26
  • 33
  • @Fung, Kindly ewview my answer below and let me know f I am wrong. Thanks. just for learning. – Tauseef Oct 08 '13 at 08:05
  • @Tauseef, It's hard to comment since you didn't provide your implementation. Try to write some code or modify answers from you links to solve OP's problem would be a good way to learn :) – Fung Oct 08 '13 at 09:00
  • :) thanks, Is it true that the xml has the root node `` as your code says `new XmlRootAttribute("xml")`. means there is a single root, not a list? yes, he posted his code upon my cooment. – Tauseef Oct 08 '13 at 10:31
0

The root of your XML is not a List, the root of your xml is the <xml> node I think you are just confused by its name :)

please visit the following link, It has many good answers voted by many people.

Here is the link: How to Deserialize XML document

Error Deserializing Xml to Object - xmlns='' was not expected

Simply take off the Namespace =:

[XmlRoot("xml"), XmlType("xml")] 
public class RegisterAccountResponse {...}
Community
  • 1
  • 1
Tauseef
  • 2,035
  • 18
  • 17