I’ve got a strange behaviour with my XML serializer.
After reading an XML and deserializing it into an object, all properties are set to it’s default values and not to the values declared in the xml file.
The serializer doesn’t throw an exception and runs properly. The xml file is properly formed and fits to the class structure.
Anyone an idea how that can be, or how I could get to the source of the problem?
Thank you
edit:
I didn’t tell you the whole story. The thing is, the XML which I get is from another component. I was able to deserialize the XML file and now I got a diffrent format. Since the file has about 3000 lines I can’t post the whole code. But here’s the difference:
deserializable:
<?xml version="1.0" encoding="utf-8"?>
<rootElem xmlns:cfg="namespace1" xmlns:office="namespace2" xmlns="namespace3">
<Prop1 xmlns="">6</Prop1>
<Prop2 xmlns="">string</Prop2>
</rootElem>
not deserializable
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<rootElem xmlns:cfg="namespace1" xmlns:office="namespace2" xmlns="namespace3">
<Prop1>6</Prop1>
<Prop2>string</Prop2>
</rootElem>
I don’t really understand why I can unmarshal the first example due to the xmlns tag inside each element and why I can’t unmarshal the second one…
edit2: just realized that just the top level elements got these strange xmlns=”” attributes. But the C# class declaration is not different from all other classes…that’s weird.
The C# class is like this:
using namespace1;
namespace namespace3
{
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="namespace3")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="namespace3", IsNullable=true)]
public partial class rootElem: BaseObject
{
[System.Xml.Serialization.XmlElementAttribute(Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
public int Prop1
{
//...
}
}
}
A concrete example (both c# ad xml) would go a very long way here. Most likely one of:
With your edit, it becomes clearer. Xml namespaces are very significant;
<foo xmlns="abc"/>and<foo/>are completely unrelated. Further, xml namespaces are inherited, so in:it is the case that
Prop1andProp2are in thenamespace3namespace that they inherit from their parent. To make it absolutely clear to the c# that you want those to be in the child namespace (rather than the empty namespace), tell it: