1

I need to deserialize the xml below. I want to use xmlserializer because I am (more) familiar with it. I believe this xml is not constructed correctly however I cannot change it. The below represents a list of category objects. When I try to deserialize using

xmlserializer(typeof(List<Category>))

I get this error: "categories xmlns='' is not expected"

<?xml version="1.0" encoding="utf-8" ?>
<categories>
  <category id="16" name="Exports" parent_id="13"/>
  <category id="17" name="Imports" parent_id="13"/>
  <category id="3000" name="Income Payments & Receipts" parent_id="13"/>
  <category id="125" name="Trade Balance" parent_id="13"/>
  <category id="127" name="U.S. International Finance" parent_id="13"/>
</categories>

I don't mind making some kind of dummy class to deserilize these if that is what I have to do. Here is my Category Class

[XmlType("category")]
 public class Category
 {


    [XmlAttribute("id")]
    public int ID { get; set; }

    [XmlAttribute("parent_id")]
    public int ParentID { get; set; }

    [XmlAttribute("name")]
    public string Name { get; set; }
}

My code:

    XmlSerializer serializer = new XmlSerializer(typeof(List<Category>));
    StringReader reader = new StringReader(xml);
    List<Category> obj = null;

    using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(reader))
    {
        obj = (List<Category>)serializer.Deserialize(xmlReader);
    }
        return obj;
Sam
  • 1,621
  • 3
  • 22
  • 30
  • I think you need to specify that 'categories' is a list of 'category'. – Edwin de Koning Jan 29 '13 at 07:34
  • I guess you need to specify the default namespace for the serializer. See: http://stackoverflow.com/questions/2500111/how-do-i-add-a-default-namespace-with-no-prefix-using-xmlserializer – Emond Jan 29 '13 at 07:36
  • I believe XmlSerializer needs a default constructor, see: http://stackoverflow.com/a/267727/1380061 – Fredrik Jan 29 '13 at 07:46

3 Answers3

3

You can just pass in the XmlRootAttribute into the serializer for the "categories" part.

BUT... you must remove the "&" from your xml because its not valid

XmlSerializer serializer = new XmlSerializer(typeof(List<Category>), new XmlRootAttribute("categories"));

using (FileStream fileStream = new FileStream(@"C:\Test.xml", FileMode.Open, FileAccess.Read, FileShare.Read))
{
    var test = serializer.Deserialize(fileStream);
}

enter image description here

Here is your method working with a String.Replace to sort out the "&"

    private List<Category> GetCategories(string xmlData)
    {
        List<Category> obj = null;
        XmlSerializer serializer = new XmlSerializer(typeof(List<Category>), new XmlRootAttribute("categories"));
        StringReader reader = new StringReader(xmlData.Replace("&","&amp;"));
        using (System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(reader))
        {
            obj = (List<Category>)serializer.Deserialize(xmlReader);
        }
        return obj;
    }
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
1

Try to make a categories class that will contain your List<Category> like this:

[XmlRoot("categories")]
public class Categories
{
    public Categories() 
    {
       Items = new List<User>();
    }

    [XmlElement("category")]
    public List<Category> Items {get;set;}
}

You can than create a serializer like this:

XmlSerializer serializer = new XmlSerializer(typeof(Categories));
Edwin de Koning
  • 14,209
  • 7
  • 56
  • 74
0

Do you have an XSD that this XML should conform to? If so, you can generate the required code using: "xsd your.xsd /classes"

Immortal Blue
  • 1,691
  • 13
  • 27