1

I have got two classes:

public class Foo
{
    public string FooName{get;set;}
    public List<Bar> Bars{get;set;}
}

public class Bar
{
    public string BarName{get;set;}
}

If I serialize this using the XML parser I come up with:

<Foo>
    <FooName>ExampleFooName</FooName>
    <Bars>
        <Bar>
             <BarName>ExampleBarName</BarName>
        </Bar>
        <Bar>
             <BarName>AnotherExampleBarName</BarName>
        </Bar>
    </Bars>
<Foo>

Is there any way to receive this as the result:

<Foo>
    <FooName>ExampleFooName</FooName>

    <Bar>
         <BarName>ExampleBarName</BarName>
    </Bar>
    <Bar>
         <BarName>AnotherExampleBarName</BarName>
    </Bar>
<Foo>

I am looking to either restructure the Foo and Bar classes, or pass in some parameter to the XMLSerializer to receive the result. I really want to avoid writing my own XML parser.

4
  • 1
    What did the Bars node do to make you reject it? Now Bars is sad. :( Bars wants to know if you'd reconsider. Bars really wants to make this work out with you. Commented Mar 14, 2013 at 21:06
  • possible duplicate of How do I get XmlSerializer to not serialize container tags? Commented Mar 14, 2013 at 21:09
  • Customer wont let me use Bars :( I'm going over that other answer now Commented Mar 14, 2013 at 21:13
  • 1
    Bars is really sorry to hear that. You've made Bars very angry and now Bars is out to get you. Your code better watch its back! Commented Mar 14, 2013 at 21:18

1 Answer 1

2
public class Foo
{
    public string FooName{get;set;}
    [XmlElement("Bar")]
    public List<Bar> Bars{get;set;}
}
Sign up to request clarification or add additional context in comments.

Comments

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.