AlfeG
AlfeG

Reputation: 1473

XML serialization question

Have an XML with next form:

 <categories someAttribute="test">
  <category id="1">
   <title></title>
  </category>
  <category id="1">
   <title></title>
  </category>
 </categories>

There is no way to change XML structure. But what I want is to replace buggy hand coded XML generation with XMLSerialization.

Please help with those Category list. Is there a way to instruct XML serializer to not wrap list of categories

Code for Example:

public class Category
{
    public int Id{get;set;}
}

public class Categories
{
    public List<Category> CategoriesList { get; set; } 
}

Upvotes: 1

Views: 139

Answers (3)

Marc Gravell
Marc Gravell

Reputation: 1063338

Like so:

public class Category
{
    [XmlAttribute("id")]
    public int Id { get; set; }

    [XmlElement("title")]
    public string Title { get; set; }
}

[XmlRoot("categories")]
public class Categories
{
    [XmlAttribute("someAttribute")]
    public string SomeValue { get; set; }

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

Upvotes: 3

XOR
XOR

Reputation: 2167

Look at XmlAnyElementAttribute.

Upvotes: 0

Peter
Peter

Reputation: 38515

with the IXmlSerializable interface you can define on your own how to write the xml or you could make your list to an array i think it would be written the way you want but im not sure..

Upvotes: -1

Related Questions