473,508 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Importing nodes without namespace

Hi, I'm having trouble with namespaces when importing nodes. I'd like to get
this output:

<exampleRoot xmlns="http://mynamespace">
<row Lsm_Info="ABC123" />
<row Lsm_Info="DEF456" />
</exampleRoot>

But instead I'm getting:

<exampleRoot xmlns="http://mynamespace">
<row Lsm_Info="ABC123" xmlns="" />
<row Lsm_Info="DEF456" xmlns="" />
</exampleRoot>

This is the code I'm using:

XmlReader reader = command.ExecuteXmlReader();

XmlElement rootNode = xmlDocument.CreateElement("exampleRoot ",
http://mynamespace);
xmlDocument.AppendChild(rootNode);

reader.MoveToContent();
XmlNode readerNode = xmlDocument.ReadNode(reader);

while (readerNode != null)
{
XmlNode importNode = xmlDocument.ImportNode(readerNode, false);

xmlDocument.DocumentElement.AppendChild(importNode );

readerNode = xmlDocument.ReadNode(reader);
}

How do I get rid of the xmlns=""? Any help is much appreciated!

Cheers,
Chris
Nov 21 '05 #1
5 10263


Christoffer wrote:
Hi, I'm having trouble with namespaces when importing nodes. I'd like to get
this output:

<exampleRoot xmlns="http://mynamespace">
<row Lsm_Info="ABC123" />
<row Lsm_Info="DEF456" />
</exampleRoot>

But instead I'm getting:

<exampleRoot xmlns="http://mynamespace">
<row Lsm_Info="ABC123" xmlns="" />
<row Lsm_Info="DEF456" xmlns="" />
</exampleRoot>


A node is not changed when it is imported or cloned thus if those row
elements you are importing are in no namespace then the serialization is
correct, an element in no namespace which is a child of an element
declaring a default namespace needs to be serialized with xmlns="" to
make sure the markup undeclares the namespace.

If you want to have elements in a different namespace then importNode
does not help, you need to create the new elements in the desired namespace.


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 21 '05 #2
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...

Christoffer wrote:
Hi, I'm having trouble with namespaces when importing nodes. I'd like to
get this output:

<exampleRoot xmlns="http://mynamespace">
<row Lsm_Info="ABC123" />
<row Lsm_Info="DEF456" />
</exampleRoot>

But instead I'm getting:

<exampleRoot xmlns="http://mynamespace">
<row Lsm_Info="ABC123" xmlns="" />
<row Lsm_Info="DEF456" xmlns="" />
</exampleRoot>


A node is not changed when it is imported or cloned thus if those row
elements you are importing are in no namespace then the serialization is
correct, an element in no namespace which is a child of an element
declaring a default namespace needs to be serialized with xmlns="" to make
sure the markup undeclares the namespace.

If you want to have elements in a different namespace then importNode does
not help, you need to create the new elements in the desired namespace.


So the "row" elements/nodes which I get from the XmlReader have to be
re-created? I can not simply change their namespace?

Cheers,
Chris
Nov 21 '05 #3


Christoffer wrote:
So the "row" elements/nodes which I get from the XmlReader have to be
re-created? I can not simply change their namespace?


You cannot change the namespaceURI of nodes in the DOM implemented in
..NET 1.x and 2.0.
The W3C DOM Level 3 suggests a method named RenameNode on XML document
objects but .NET does not implement the W3C DOM Level 3, only level 2
(and lots of Microsoft extensions like InnerXml/OuterXml).

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 21 '05 #4
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...

Christoffer wrote:

You cannot change the namespaceURI of nodes in the DOM implemented in


Basically, it can't be done without loads and loads of work right?

Is there a way to get the StartElement and EndElement from an XmlElement? In
that case I could create the xml element and then build a string instead of
using XmlDocument.

For example,

string xmlRows = command.ExecuteScalar() as string;
XmlElement rootNode = xmlDocument.CreateElement("exampleRoot ",
"http://mynamespace");
StringBuilder build = new StringBuilder();
build.Append(rootNode.StartElement);
build.Append(xmlRows);
build.Append(rootNode.EndElement);

That would, in theory, create the result I'd like to get...

Cheers,
Chris
Nov 21 '05 #5


Christoffer wrote:
"Martin Honnen" <ma*******@yahoo.de> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Christoffer wrote:

You cannot change the namespaceURI of nodes in the DOM implemented in

Basically, it can't be done without loads and loads of work right?


It can be done with a few lines of code, as said if you have DOM nodes
you just need to make sure you create the same nodes with the desired
namespace(s). As only element nodes and attribute nodes can be in a
namespace you have to write code for those two node types, for all other
nodes you can clone/import/copy as usual.
However you have XmlReader you start with so there it might not make
sense to first use ReadNode to create DOM nodes and then rework the DOM
with CreateElement but to implement an extension of XmlTextReader that
simply changes the namespace(s) as needed.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 21 '05 #6

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

Similar topics

5
5257
by: Adam Barr | last post by:
I have a tag foo that I want to copy unchanged when it is a subtag of bar, so I have a template (x is the namespace for the document): <xsl:template match="x:bar/x:foo"> <xsl:copy>...
1
2212
by: Steve George | last post by:
Hi, I have a scenario where I have a master schema that defines a number of complex and simple types. I then have a number of other schemas (with different namespaces) where I would like to reuse...
6
3498
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"...
2
10663
by: Greg | last post by:
Hi. I have a rather large xml document (object) that can have one or more nodes with a certain attribute throughout (at ANY depth, not at the same level necessarily). I need to find this...
29
4178
by: Natan | last post by:
When you create and aspx page, this is generated by default: using System; using System.Collections; using System.Collections.Specialized; using System.Configuration; using System.Text; using...
0
847
by: google | last post by:
Hi, I currently have a .net page default.aspx - as so: <%@ Page Inherits="webmail.telnet_test" Src="./mail_code/telnet.cs" Trace="true" Language="C#" %> <html> <head>
2
4096
by: daz_oldham | last post by:
Hi Everyone I have an xml document, and for the purposes of my application, I am going to take a node of the xml (child), add a child to it and then store it into the database for later use. ...
3
4122
by: 0to60 | last post by:
Please help! I'm using the following code to get an XML doc: string str = "http://api.local.yahoo.com/MapsService/V1/geocode?appid=12345&city=addison"; System.Net.HttpWebRequest request =...
3
1366
by: thesmithman | last post by:
Howdy folks, My XML file contains the contents of several web pages. Example: <allPages> <thisPage> <h1>Page Title</h1> <p>Some text, which contains an <a href="link.php">inline...
0
7231
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
7336
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
7401
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...
1
7063
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
5640
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
4720
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...
0
3211
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...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
432
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...

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.