473,608 Members | 2,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlSerializerNa mespaces 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="myName Space">
<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;

'------------------------------------------------------------------------------
' <autogenerate d>
' 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.Seri alization

'
'This source code was auto-generated by xsd, Version=1.1.432 2.2032.
'

'<remarks/>
<System.Xml.Ser ialization.XmlT ypeAttribute([Namespace]:="http://schemas.xmlsoap .org/soap/envelope/"),
_

System.Xml.Seri alization.XmlRo otAttribute([Namespace]:="http://schemas.xmlsoap .org/soap/envelope/",
IsNullable:=fal se)> _
Public Class Envelope

'<remarks/>
<System.Xml.Ser ialization.XmlE lementAttribute ("Body")> _
Public Items() As EnvelopeBody
End Class

'<remarks/>
<System.Xml.Ser ialization.XmlT ypeAttribute([Namespace]:="http://schemas.xmlsoap .org/soap/envelope/")>
_
Public Class EnvelopeBody

'<remarks/>

<System.Xml.Ser ialization.XmlE lementAttribute ([Namespace]:="myNameSpace" )>
_
Public TopElm As TopElm
End Class

'<remarks/>
<System.Xml.Ser ialization.XmlT ypeAttribute([Namespace]:="myNameSpace" ),
_
System.Xml.Seri alization.XmlRo otAttribute([Namespace]:="myNameSpace" ,
IsNullable:=fal se)> _
Public Class TopElm

'<remarks/>

<System.Xml.Ser ialization.XmlE lementAttribute (Form:=System.X ml.Schema.XmlSc hemaForm.Unqual ified)>
_
Public Elm1 As String

'<remarks/>

<System.Xml.Ser ialization.XmlE lementAttribute (Form:=System.X ml.Schema.XmlSc hemaForm.Unqual ified)>
_
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.Seri alization

Module Module1

Sub Main()

Dim ns As New XmlSerializerNa mespaces
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", "myNameSpac e") 'THIS LINE = PROBLEM?

Dim mySerializer As XmlSerializer
mySerializer = New XmlSerializer(G etType(Envelope ))

Dim myEnvelope As New Envelope
Dim myEnvelopeBody As New EnvelopeBody
Dim myTopElm As New TopElm

myTopElm.Elm1 = "Hello"
myTopElm.Elm2 = "World"

myEnvelope.Item s = New EnvelopeBody() {myEnvelopeBody }
myEnvelopeBody. TopElm = myTopElm

Dim myWriter As New StreamWriter("x sdClass.xml")
mySerializer.Se rialize(myWrite r, myEnvelope, ns)
myWriter.Close( )

End Sub

End Module

--------------------------------------

ALAS THE PROBLEM:

The following line of code;

ns.Add("m", "myNameSpac e") '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="myNam eSpace").
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="myName Space")>

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="myName Space"
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 2792

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

Similar topics

6
4233
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 xmlns:NAME='old_namespace'> <NAME:element>...</NAME:element> <NAME:element>...</NAME:element>
6
8338
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 the dreaded <elementname xmlns=''> was not expected exception. If I comment out the XmlTypeAttribute, it works just fine. Just so you know, when I instantiate an instance of an XmlSerializer, I pass a default namespace to the ctor. ...
6
3512
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" and "ns0:Company" but I do not want to delete their child nodes("w:p","w:r","w:t"). How can I do this? <ns0:Proposal> <ns0:Company> <w:p> <w:r>
2
1553
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 copied elements appear with an empty default namespace appearing on them (xmlns=""). The root element of the output XML does declare a default namespace (xmlns="urn:schemas-microsoft-com:office:spreadsheet").
4
2254
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 already created 2 namespace managers
8
2025
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 is as follows: /** * Print an element node, and, by recursive descent, it's children * * @param node the node to print
1
2429
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 namespace output XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
2
21553
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: http://msdn2.microsoft.com/en-gb/library/system.xml.serialization.xmlnamespacedeclarationsattribute.aspx I get namespace prefixes in the document, but not on the root node. Specifically, the serializer does not work as described in the last
4
1663
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. example: <root xmlns="http://foo1" xmlns:a="http://foo2"> .... </root>
0
8010
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8501
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8483
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8157
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8349
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6820
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
2477
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1607
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1336
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.