Hello,
I've come up against a bit of a brick wall with Microsoft's .net XmlSerializer. I'm trying to deserialize some XML into an object, which is fine if I'm using a single object, but the problem comes when one puts an object into a List and tries to serialize/deserialize that. First up, here's a sample C# windows console program to illustrate the problem:
If the class 'Foo' is serialized as a root element, things behave fine, and as expected - the JezNamespace xmlns is applied to the root Foo element, and deserialization occurs fine. However if I create a List and serialize that, the XmlSerializer: - Creates a root element of ArrayOfFoo - Puts the Foo elements as children of that element - Sets the xmlns of EVERY child of Foo to the JezNamespace namespace!
I'm OK with the first two, but the third one seems mad... maybe a bug in XmlSerializer? Is there some way I can deal with this behaviour? I don't want to be specifying my namespace for every child of Foo, I just want to specify it for Foo. If I do that, currently, XmlSerializer doesn't deserialize the class properly - it just skips over any Foo element with the JezNamespace xmlns set. I have to set ALL the child elements to have that xmlns.
What I'd like to get to is XmlSerializer generating something like:
<ArrayOfFoo>
<Foo xmlns="http://schemas.datacontract.org/2004/07/JezNamespace">
<Field1>hello</Field1>
<Field2>world</Field2>
</Foo>
<Foo xmlns="http://schemas.datacontract.org/2004/07/JezNamespace">
<Field1>aaa</Field1>
<Field2>bbb</Field2>
</Foo>
</ArrayOfFoo>
... and then have XmlSerializer be able to deserialize that properly into a List. Any ideas how I can get it doing this?