473,573 Members | 2,889 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 XmlNamespaceDec larationsAttrib ute to the class that
represents the root element."

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

Is this a bug or a feature?

Thanks -Scott

Feb 28 '07 #1
2 21537
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:

XmlSerializerNa mespaces namespaces = new XmlSerializerNa mespaces();
namespaces.Add( "prefix", "http://example.com/2007/ns1");
XmlSerializer serializer = new XmlSerializer(t ypeof(Example)) ;

Example example = new Example();
example.Name = "Kibo";
example.Power = 42;
serializer.Seri alize(Console.O ut, example, namespaces);
Console.WriteLi ne();

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

<prefix:Examp le xmlns:prefix="h ttp://example.com/2007/ns1">
<prefix:Name>Ki bo</prefix:Name>
<prefix:Power>4 2</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(Namespa ce = "http://example.com/2007/ns1")]
public class Example {
public Example () {}
[XmlElement(Name space = "")]
public string Name;
[XmlElement(Name space = "")]
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
5832
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
1717
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: <rdf:RDF xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"...
2
9292
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" ); elem.InnerXml = "<first>abcde</first><last>1234</last>";
6
3509
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>
0
2787
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) that i need to create for this post. <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"...
4
2251
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
4
1661
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
2792
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 namespace. Some schemas extend complex types in others. When i get a soap:Fault from my test server it could contain a serialized exception object...
11
2297
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"> <URI>http://acs.amazonaws.com/groups/s3/LogDelivery</URI> </Grantee> However I am unclear on how to add these types of attributes. The following...
0
7992
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. ...
0
8190
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...
1
7756
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...
0
6385
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...
1
5571
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5281
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3722
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3722
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1027
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...

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.