Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extra xmlns in xml generated from xmlserializer

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.

like image 853
adrianm Avatar asked Jan 29 '26 13:01

adrianm


1 Answers

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 tag1 has an empty namespace set (or it implicitly infers it has an empty namespace) and that myroot has the namespace "blabla".

The solution is to make sure that tag1 has 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.

like image 62
Pieter van Ginkel Avatar answered Jan 31 '26 06:01

Pieter van Ginkel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!