I have generated a class from a schema via the xsd.exe tool
Now when I serialize the class I get an extra xmlns="" on everything below the root.
<myroot xmlns="blabla">
<tag1 xmlns="">
<tag2>
...
The schema looks like:
<xsd:schema xmlns="blabla" targetNamespace="blabla" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="MyRoot">
I can’t see anything in the schema indicating that the tag1 element and below should be in another namespace than the root.
The serialization code just does:
MyRoot doc = new MyRoot();
...
XmlSerializer xs = new XmlSerializer(typeof(MyRoot));
MemoryStream ms = new MemoryStream();
try {
xs.Serialize(ms, doc);
The class generated from xsd.exe only contains a namespace attribute on the root class.
What it’s doing here is overriding the namespace
"blabla"from the parent element with a specific namespace"".The reason it’s doing here is because
tag1has an empty namespace set (or it implicitly infers it has an empty namespace) and thatmyroothas the namespace"blabla".The solution is to make sure that
tag1has the same namespace as"blabla".I know, this is very irritating because it means you have to provide the namespace again and again, but that’s how it works.