473,407 Members | 2,326 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,407 software developers and data experts.

Adding namespace prefix to Root node

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/lib...attribute.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
paragraph:

(from http://msdn2.microsoft.com/en-gb/lib...attribute.aspx)

" For example, in the following XML document, only the prefix pair
"cal" is captured, but not the "x" prefix. To get that data, add a
member with the XmlNamespaceDeclarationsAttribute to the class that
represents the root element."

If you look through the sample, they already use the
XmlNamespaceDeclarationsAttribute and there is no way to associate the
instance of the XmlNamespaceDeclarationsAttribute with the root node.

Is this a bug or a feature?

Thanks -Scott

Feb 28 '07 #1
2 21464
Ok..figured it out...you have to load the resulting document into an
XmlDocument type, then use the DocumentElement.Prefix property to set
the prefix of the root node...works perfectly. It's unfortunate to
have to do that since that is a common scenario.

Feb 28 '07 #2
sc******@gmail.com wrote:
I want to add a namespace prefix to the root node of an object I am
serializing to XML.
Here is an example doing that:

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("prefix", "http://example.com/2007/ns1");
XmlSerializer serializer = new XmlSerializer(typeof(Example));

Example example = new Example();
example.Name = "Kibo";
example.Power = 42;
serializer.Serialize(Console.Out, example, namespaces);
Console.WriteLine();

[XmlRoot(Namespace = "http://example.com/2007/ns1")]
public class Example {
public Example () {}
public string Name;
public int Power;
}
The output is:

<prefix:Example xmlns:prefix="http://example.com/2007/ns1">
<prefix:Name>Kibo</prefix:Name>
<prefix:Power>42</prefix:Power>
</prefix:Example>
Is that what you want to achieve? Or do you want the namespace only on
the root element? Then you need to use e.g.

[XmlRoot(Namespace = "http://example.com/2007/ns1")]
public class Example {
public Example () {}
[XmlElement(Namespace = "")]
public string Name;
[XmlElement(Namespace = "")]
public int Power;
}
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Mar 1 '07 #3

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

Similar topics

4
by: Andreas Håkansson | last post by:
I have a price of XML that looks like this <Root> <SomeNode> ..... </SomeNode> <Signature xmlns="http://www.w3.org/2000/09/xmldsig#"> ... </Signature> </Root>
3
by: jwalton | last post by:
This is so silly I must be doing something wrong! I need to insert (using VB.NET) a new node in a DOM; node has a namespace. I first create, under programme, basic structure, starting thus: ...
2
by: Maersa | last post by:
hi, i'm trying to add "attributes" to some nodes and it works at design time but fails at runtime. XmlDocument doc = new XmlDocument(); XmlElement elem = doc.CreateElement( "root" );...
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"...
0
by: richwangler | last post by:
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)...
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...
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: theonlydavewilliams | last post by:
Hi there I'm hoping there's an easy answer to a (hopefully) not too long-winded issue... I'm building a C# web client using a proxy wsdl.exe'd from a wsdl file and six schemas, each in a different...
11
by: =?Utf-8?B?TTFpUw==?= | last post by:
I’m trying to add the following attributes (xmlns:xsi and xsi:type) to a node (Grantee) like the following: <Grantee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Group">...
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: 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
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
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,...
0
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...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.