473,387 Members | 1,834 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,387 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 2761

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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.