473,657 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Inserting Xml Node and maintaining the prefix

GR
Hi,
I have something like the following:
(Please note this not actual code and it's very rough)

string source = "<abc:Root
xmlns:abc="http ://mynamespace.com ><abc:Record/></abc:Root>"
XmlDocument doc = new XmlDocument();
doc.LoadXml(sou rce);

I now want to add a node such as the following:
XmlDocument tempDoc = new XmlDocument();
tempDoc.LoadXml ("<abc:Order/>");
doc.FirstChild. FirstChild.Appe ndChild(tempDoc .FirstChild);

But when I do this the "abc:" is stripped out of the xml.
What I'm after is to add a new node to the 'doc' document and for it to
have the "abc:" prefix. I've tried various ways (create nodes, create
document fragment, create xmldoc etc) but all the methods I've tried so
far either strip the "abc:", crash or include the "abc:" but also with
the entire namespaceuri (which I dont want either).

So I have the following:
"<abc:Root xmlns:abc="http ://mynamespace.com ><abc:Record/></abc:Root>"

Now using C# I want to add a new node so that the doc.OuterXml looks
like the following:
"<abc:Root
xmlns:abc="http ://mynamespace.com ><abc:Record><a bc:Order/></abc:Record></abc:Root>"

If possible please post a full code snippet.
Thanks

Nov 24 '06 #1
1 14252
GR wrote:
string source = "<abc:Root
xmlns:abc="http ://mynamespace.com ><abc:Record/></abc:Root>"
XmlDocument doc = new XmlDocument();
doc.LoadXml(sou rce);
What I'm after is to add a new node to the 'doc' document and for it to
have the "abc:" prefix.
It is simple, just use a namespace aware overload of CreateElement and
pass in the namespace URI you want to create the element in e.g.
string source =
"<abc:Root xmlns:abc=\"htt p://mynamespace.com \"><abc:Reco rd/></abc:Root>";
XmlDocument doc = new XmlDocument();
doc.LoadXml(sou rce);

XmlElement order = doc.CreateEleme nt("abc:Order" ,
doc.DocumentEle ment.NamespaceU RI);
doc.DocumentEle ment.AppendChil d(order);
doc.Save(Consol e.Out);

Result is

<abc:Root xmlns:abc="http ://mynamespace.com ">
<abc:Record />
<abc:Order />
</abc:Root>
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 25 '06 #2

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

Similar topics

3
1720
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#" xmlns:prism="http://prismstandard.org/namespaces/1.2/basic/" xmlns:pcv="http://prismstandard.org/namespaces/1.2/pcv/"> Then I attempt to...
0
1487
by: JMD | last post by:
I have a set of schemas for two different XML files that share common structures through the use of "import". Eventually, the main XSD file (which just gathers pieces from the common stuff to show what goes in this root element) contains: <xs:element name= "ImageBox" type="ns:ImageBox_t" form="qualified" /> I use a validating reader to pull this into an XmlDocument. Then, step
3
6896
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when inserting/deleting rows from a large table. My scenario: Table (lets call it FACT1) with 1000 million rows distributed on 12
3
4857
by: Sharon | last post by:
I'm trying to navigate inside an XML file that has a very simple namespace, But when I'm using the name space a get nothing (Count == 0) when I do SelectNodes() or SelectSingleNode() (null). When I remove the namespace from the XML file and from the code --> the Select is successful but I do need to leave the namespace in the XML file. Here is the XML file I'm using: <?xml version="1.0" standalone="yes"?> <MyDB xmlns="DataBase.xsd">...
1
1266
by: John Salerno | last post by:
If I want to create a button that adds text boxes to a form, how would I write the code so that text boxes can be inserted between text boxes that already exist? For example, if this displays properly: Main node 1 - child node (A) - child node (B) - child node
2
21575
by: scottpet | last post by:
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/library/system.xml.serialization.xmlnamespacedeclarationsattribute.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
3
2260
by: Jeff | last post by:
Hi all, I'm wondering if there is a function that will return the xpath to a specific node given its context node. Essentially, I want the reverse of the document.evaluate functionality. I've seen examples where people build up the xpath themselves by walking the DOM, but I thought there has to be a better way. I've included a simple example of this, based on code from the Solvent project at MIT. This code works reasonably well, but...
4
2357
by: matth | last post by:
I've been working on something that deals with handling a user's selection within the DOM and I'm tripping up on one last, but crucial, detail. Forgive me for the length of the code, but my question is pretty straightforward and my brain hasn't been working. Problem: The way I'm iterating through the nodes doesn't allow me to preserve the node hierarchy. Instead of:
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8319
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8512
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8612
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5638
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4171
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2739
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 we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1732
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.