This problem should be easily reproducable if anybody has the time. I
need to build the following XML programatically and decided to use the
XMLSerializer. I simplified the XML (myExample.xml) that i need to
create for this post.
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<m:TopElm xmlns:m="myNameSpace">
<Elm1>Hello</Elm1>
<Elm2>World</Elm2>
</m:TopElm>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
----------------------------
Here's what I've done:
[STEP 1] I used the XSD.exe tool to generate the .xsd files.
xsd.exe myExample.xml
It created two separate .xsd files (myExample.xsd & myExample_app1.xsd)
[STEP 2] I used the XSD.exe tool again to generate a class from these
newly generated .xsd files.
xsd.exe myExample.xsd myExample_app1.xsd /classes /language:vb
It created a vb file containing the following code;
'------------------------------------------------------------------------------
' <autogenerated>
' This code was generated by a tool.
' Runtime Version: 1.1.4322.2032
'
' Changes to this file may cause incorrect behavior and will be
lost if
' the code is regenerated.
' </autogenerated>
'------------------------------------------------------------------------------
Option Strict Off
Option Explicit On
Imports System.Xml.Serialization
'
'This source code was auto-generated by xsd, Version=1.1.4322.2032.
'
'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://schemas.xmlsoap.org/soap/envelope/"),
_
System.Xml.Serialization.XmlRootAttribute([Namespace]:="http://schemas.xmlsoap.org/soap/envelope/",
IsNullable:=false)> _
Public Class Envelope
'<remarks/>
<System.Xml.Serialization.XmlElementAttribute("Bod y")> _
Public Items() As EnvelopeBody
End Class
'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://schemas.xmlsoap.org/soap/envelope/")>
_
Public Class EnvelopeBody
'<remarks/>
<System.Xml.Serialization.XmlElementAttribute([Namespace]:="myNameSpace")>
_
Public TopElm As TopElm
End Class
'<remarks/>
<System.Xml.Serialization.XmlTypeAttribute([Namespace]:="myNameSpace"),
_
System.Xml.Serialization.XmlRootAttribute([Namespace]:="myNameSpace",
IsNullable:=false)> _
Public Class TopElm
'<remarks/>
<System.Xml.Serialization.XmlElementAttribute(Form :=System.Xml.Schema.XmlSchemaForm.Unqualified)>
_
Public Elm1 As String
'<remarks/>
<System.Xml.Serialization.XmlElementAttribute(Form :=System.Xml.Schema.XmlSchemaForm.Unqualified)>
_
Public Elm2 As String
End Class
[STEP 3]
I then created a console application that included this newly generated
file. The console application code looks as follows;
Imports System.IO
Imports System.Xml.Serialization
Module Module1
Sub Main()
Dim ns As New XmlSerializerNamespaces
ns.Add("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/")
ns.Add("SOAP-ENC", "http://schemas.xmlsoap.org/soap/encoding/")
ns.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance")
ns.Add("xsd", "http://www.w3.org/2001/XMLSchema")
ns.Add("m", "myNameSpace") 'THIS LINE = PROBLEM?
Dim mySerializer As XmlSerializer
mySerializer = New XmlSerializer(GetType(Envelope))
Dim myEnvelope As New Envelope
Dim myEnvelopeBody As New EnvelopeBody
Dim myTopElm As New TopElm
myTopElm.Elm1 = "Hello"
myTopElm.Elm2 = "World"
myEnvelope.Items = New EnvelopeBody() {myEnvelopeBody}
myEnvelopeBody.TopElm = myTopElm
Dim myWriter As New StreamWriter("xsdClass.xml")
mySerializer.Serialize(myWriter, myEnvelope, ns)
myWriter.Close()
End Sub
End Module
--------------------------------------
ALAS THE PROBLEM:
The following line of code;
ns.Add("m", "myNameSpace") 'THIS LINE = PROBLEM?
creates a prefix for myNameSpace properly but then proceeds to move the
namespace to the root in the Envelope node (xmlns:m="myNameSpace").
Every thing is correct except that I want the <m:TopElm> tag to contain
this namespace definition and for it not to be moved to the root. e.g.
<m:TopElm xmlns:m="myNameSpace")>
Here is what actually is outputed;
<?xml version="1.0" encoding="utf-8"?>
<SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:m="myNameSpace"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Body>
<m:TopElm>
<Elm1>Hello</Elm1>
<Elm2>World</Elm2>
</m:TopElm>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
Thank you for your time!