472,131 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,131 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 21140
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Andreas Håkansson | last post: by
2 posts views Thread by Maersa | last post: by
6 posts views Thread by Nikhil Patel | last post: by
4 posts views Thread by Karine Bosch | last post: by
4 posts views Thread by =?iso-8859-1?q?S=E9bastien_Ros?= | last post: by
11 posts views Thread by =?Utf-8?B?TTFpUw==?= | last post: by
reply views Thread by leo001 | last post: by

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.