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

2
  • 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. Commented Jan 23, 2011 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... Commented Jan 24, 2011 at 18:23

2 Answers 2

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,
}
Sign up to request clarification or add additional context in comments.

1 Comment

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
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.

2 Comments

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
It is possible, and not difficult - see the accepted answer for stackoverflow.com/questions/2006482/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.