473,245 Members | 1,400 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,245 software developers and data experts.

XmlSerializerNamespaces moves namespace to root node

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!

Nov 12 '05 #1
0 2754

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: insane79 | last post by:
Hi, i have one problem with an xsl trasformation (I'm using Xalan) The source XML document is the following: <?xml version = '1.0' encoding = 'ISO-8859-1'?> <ROOT...
6
by: David B. Bitton | last post by:
I am having a problem deserializing XML when the root node is missing a namespace declaration. My Type has an XmlTypeAttribute with a namespace defined. If I attempt to deserialize the XML, I get...
6
by: Nikhil Patel | last post by:
Hi all, Following is a portion of an XML document. I need to remove all nodes that belong to ns0 without deleting their child nodes. So in the following example , I want to delete "ns0:Proposal"...
2
by: Simon_Keep | last post by:
Hi, I have an Xslt extension object that returns a node-set to the xslt stylesheet which then uses an xsl:copy-to statement to write it to the output. The problem I am having is that the...
4
by: Karine Bosch | last post by:
Hi, My xml document uses different namespaces: <x:root xmlns:x="x:ns:meta/"> <element1 xmlns:adam="http://ns.alfaprint.be/"> </root> This gives problems when using an XPath query. I...
8
by: Simon Brooke | last post by:
I was debugging a new XML generator tonight and trying to determine why it wasn't working; and realised my dom printer does not output XML namespace declarations. My method to output an Element...
1
by: Rahul | last post by:
Hi, I am having trouble serializing an object in dotnet 2.0. I am using theis method public static string Serialize(object o, string defaultNamespace) { //create empty namespace to prevent...
2
by: scottpet | last post by:
Hi, I want to add a namespace prefix to the root node of an object I am serializing to XML. I have been reading though this article:...
4
by: =?iso-8859-1?q?S=E9bastien_Ros?= | last post by:
I have to process the xmlns declarations in the root node of a document. Unfortunately, they are not recognized as elements nor as attributes. Even with a /root/node() I can't reach them. ...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.