11

I'm trying to get to this result while serializing XML

<Test>
  <Category>
    <FileName>C:\test.txt</FileName>
    <!-- Note that here this is an array of a simple class with two fields 
         without root -->
    <Prop1>1</Prop1>
    <Prop2>2</Prop2>

    <Prop1>4</Prop1>
    <Prop2>5</Prop2>
    <!-- End array -->
  </Category>
</Test>

I already try different things like this

[Serializable]
[XmlRoot("Test")]
public class Test
{
    [XmlElement("Category")]
    public List<Category> Category= new List<Category>();
}

[Serializable]
[XmlRoot("Category")]
public class Category
{
    [XmlElement("FileName")]
    public string FileName { get; set; }

    [XmlElement("Property")]
    public List<Property> Properties = new List<Property>();
}

[Serializable]
public class Property
{
    public string Prop1 { get; set; }
    public string Prop2 { get; set; }
}

But I still get this output:

<Test>
  <Category>
    <FileName>C:\test.txt</FileName>
    <Property>
      <Prop1>1</Prop1>
      <Prop2>2</Prop2>
    </Property>
    <Property>
      <Prop1>4</Prop1>
      <Prop2>5</Prop2>
    </Property>
  </Category>
</Test>

How can I remove the Property tag ?? Thanks a lot in advance

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Khoumbe
  • 111
  • 1
  • 4
  • 2
    The XML your are trying to achieve seems pretty ambiguous to me. Having two child nodes with the same name at the same level doesn't look correct. – Darin Dimitrov Jan 23 '11 at 15:13
  • First thanks Yads for your reply I get this file from an external source and I agree with you that it's not a good xml file but as you can imagine we already ask to the editor of the faulty source to correct their output... – Khoumbe Jan 24 '11 at 18:23

2 Answers2

6

In case if you really need the exact output, as specified above, you can use workaround like this:

[Serializable]
public partial class Test {
    public List<Category> Category;
}

[Serializable]
public partial class Category {
    [XmlElement("FileName")]
    public string FileName;

    [XmlElement("Prop1")]
    [XmlElement("Prop2")]
    [XmlChoiceIdentifier("propName")]
    public string[] Properties;

    [XmlIgnore]
    public PropName[] propName;
}

public enum PropName {
    Prop1,
    Prop2,
}
andrey.ko
  • 852
  • 1
  • 7
  • 17
  • Andrey I want to thank you you for this workaround I Implement it and it does the stuff I knwo that it's not a clean solution but I will use time the Xml Source editor correct their output Thansk again 1000 times – Khoumbe Jan 24 '11 at 19:48
2

No, that is not possible without making things complex. One option is to implement IXmlSerializable, which is hard to get 100% right. You might also be able to so it by creating two subtypes, using the type-based versions of [XmlArrayItem], and hacking the model to pieces. Frankly I don't think that is worth it either.

My personal preference here would be to either choose a different layout, or use LINQ-to-XML. This is not a good it for XmlSerializer.

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Thanks Marc for your reply I planned to implement an IXmlSerializable solution but finally the workaround with the XmlChoiceIdentifier works well Thanks again for your reply – Khoumbe Jan 24 '11 at 19:46
  • 1
    It is possible, and not difficult - see the accepted answer for http://stackoverflow.com/questions/2006482/c-sharp-xml-serialization-disable-rendering-root-element-of-array – Konrad Morawski Sep 12 '12 at 10:15