473,473 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Create 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 10240


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
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
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
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
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
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
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
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
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.