Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Serialized xml is missing some namespaces #303

Closed
fabsenet opened this issue Feb 1, 2022 · 3 comments
Closed

Serialized xml is missing some namespaces #303

fabsenet opened this issue Feb 1, 2022 · 3 comments

Comments

@fabsenet
Copy link
Contributor

fabsenet commented Feb 1, 2022

I noticed I am getting wrong namespaces from the xmlserializer result. I have:

image

and as far as I can tell, the generated classes and attributes have all the correct namespaces in their attributes, but if I serialize an instance, i get:

image
But the UCI element is not in the empty namespace, so this is wrong!

the generated code for the relevant section:
image

image

I noticed, if I change the marked location to qualified, the deserialized document gets the correct namespaces written.

I am not sure how to proceed here. Did I do something wrong or is this a bug in the xml serialzer?

Help?!

@mganss
Copy link
Owner

mganss commented Feb 1, 2022

Can you post a link to the schema here?

@fabsenet
Copy link
Contributor Author

fabsenet commented Feb 2, 2022

While creating this minimal sample I think I found the real cause.

First here is the sample as simple as it can get:
xsdsample.zip

A valid instance from my expectations should look like this:

<ns0:SampleRoot xmlns:ns0="SampleNamespace">
  <Direct>
    <Direct1>Direct1_0</Direct1>
  </Direct>
  <ns0:ViaRef> <!--  <<<<<<<<<<<<<<<<<<<<<<<<<<< look here -->
    <ViaRef1>ViaRef1_0</ViaRef1>
  </ns0:ViaRef>
</ns0:SampleRoot>

The given sample generates this:

<?xml version="1.0"?>
<SampleRoot xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="SampleNamespace">
  <Direct xmlns="">
    <Direct1>direct1</Direct1>
  </Direct>
  <ViaRef xmlns=""> <!--  <<<<<<<<<<<<<<<<<<<<<<<<<<< look here -->
    <ViaRef1>viaref1</ViaRef1>
  </ViaRef>
</SampleRoot>

Notice ViaRef beeing in the wrong namespace!

For easy reference, the schema (also in the sample zip) is:

<?xml version="1.0" encoding="utf-16"?>
<xs:schema xmlns="SampleNamespace" 
xmlns:b="http://schemas.microsoft.com/BizTalk/2003"
 targetNamespace="SampleNamespace" version="1.0"
 xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="SampleRoot">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Direct">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Direct1" type="xs:string" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element ref="ViaRef" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="ViaRef">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="ViaRef1" type="xs:string" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

...so I compared the generated classes from XmlSchemaClassGenerator against one from xsd.exe and I noticed, the major difference is in the attributes of the Property ViaRef! xsd.exe is not generating [System.Xml.Serialization.XmlElementAttribute("ViaRef", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)] at all.

If I comment that line and run the test again, all the expectations are met! See comment here:

    
    [System.CodeDom.Compiler.GeneratedCodeAttribute("XmlSchemaClassGenerator", "2.0.634.0")]
    [System.SerializableAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute("SampleRoot", Namespace="SampleNamespace", AnonymousType=true)]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlRootAttribute("SampleRoot", Namespace="SampleNamespace")]
    public partial class SampleRoot
    {
        
        [System.ComponentModel.DataAnnotations.RequiredAttribute()]
        [System.Xml.Serialization.XmlElementAttribute("Direct", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public SampleRootDirect Direct { get; set; }
        
        [System.ComponentModel.DataAnnotations.RequiredAttribute()]
        //[System.Xml.Serialization.XmlElementAttribute("ViaRef", Form=System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public ViaRef ViaRef { get; set; }
    }

could it be that "easy" to not generate the XmlElementAttribute if the Element is referenced via ref="..." ?

fabsenet pushed a commit to fabsenet/XmlSchemaClassGenerator that referenced this issue Feb 2, 2022
@mganss
Copy link
Owner

mganss commented Feb 2, 2022

Fixed via #304

@mganss mganss closed this as completed Feb 2, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants