Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old February 28th, 2007, 09:45 PM
scottpet@gmail.com
Guest
 
Posts: n/a
Default 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

  #2  
Old February 28th, 2007, 10:15 PM
scottpet@gmail.com
Guest
 
Posts: n/a
Default Re: Adding namespace prefix to Root node

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.

  #3  
Old March 1st, 2007, 01:25 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Adding namespace prefix to Root node

scottpet@gmail.com wrote:
Quote:
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/
 

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles