473,396 Members | 1,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

XML element addition

I used
XmlDocument to load from a file.

Then how do I append an element to the end?

<files>
<file>
<name>graphic.txt</name>
<location>c:\temp</location>
</file>
</files>

I want to add this:
<file>
<name>editor.exe</name>
<location>c:\executable</location>
</file>
Feb 18 '07 #1
4 9246
Hi Alan

The code below assumes xml contains the original xml data and will add anode under the files node
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode fileNode = doc.CreateNode(XmlNodeType.Element, "file", doc.NamespaceURI);
XmlNode nameNode = doc.CreateNode(XmlNodeType.Element, "name", doc.NamespaceURI);
XmlNode locationNode = doc.CreateNode(XmlNodeType.Element, "location",doc.NamespaceURI);

nameNode.InnerText = "editor.exe";
locationNode.InnerText = @"C:\executable";

fileNode.AppendChild(nameNode);
fileNode.AppendChild(locationNode);

doc["files"].AppendChild(fileNode);
If you have a separate method creating the xml nodes you can rewrite it to
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode node = doc["files"].AppendChild(CreateElement("file", "", doc));
node.AppendChild(CreateElement("name", "editor.exe", doc));
node.AppendChild(CreateElement("location", @"C:\executable", doc));

....

private XmlNode CreateElement(string name, string value, XmlDocument doc)
{
XmlNode node = doc.CreateNode(XmlNodeType.Element, name, doc.NamespaceURI);
node.InnerText = value;

return node;
}
The trick is to position yourself before adding any data to select a node you can do

XmlNode node = doc["files"];

or

XmlNode node = doc.SelectSingleNode("files");

The last way is useful if for instance you want to get all the file nodes

XmlNodeList list = doc.SelectNodes("files/file");

On Sun, 18 Feb 2007 12:54:13 +0100, Alan T <al*************@yahoo.com.auwrote:
I used
XmlDocument to load from a file.

Then how do I append an element to the end?

<files>
<file>
<name>graphic.txt</name>
<location>c:\temp</location>
</file>
</files>

I want to add this:
<file>
<name>editor.exe</name>
<location>c:\executable</location>
</file>


--
Happy coding!
Morten Wennevik [C# MVP]
Feb 18 '07 #2
Thanks alot, it works very well.

"Morten Wennevik" <Mo************@hotmail.comwrote in message
news:op.tnx7maidklbvpo@stone...
Hi Alan

The code below assumes xml contains the original xml data and will add a
node under the files node
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode fileNode = doc.CreateNode(XmlNodeType.Element, "file",
doc.NamespaceURI);
XmlNode nameNode = doc.CreateNode(XmlNodeType.Element, "name",
doc.NamespaceURI);
XmlNode locationNode = doc.CreateNode(XmlNodeType.Element, "location",
doc.NamespaceURI);

nameNode.InnerText = "editor.exe";
locationNode.InnerText = @"C:\executable";

fileNode.AppendChild(nameNode);
fileNode.AppendChild(locationNode);

doc["files"].AppendChild(fileNode);
If you have a separate method creating the xml nodes you can rewrite it to
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode node = doc["files"].AppendChild(CreateElement("file", "", doc));
node.AppendChild(CreateElement("name", "editor.exe", doc));
node.AppendChild(CreateElement("location", @"C:\executable", doc));

....

private XmlNode CreateElement(string name, string value, XmlDocument doc)
{
XmlNode node = doc.CreateNode(XmlNodeType.Element, name, doc.NamespaceURI);
node.InnerText = value;

return node;
}
The trick is to position yourself before adding any data to select a node
you can do

XmlNode node = doc["files"];

or

XmlNode node = doc.SelectSingleNode("files");

The last way is useful if for instance you want to get all the file nodes

XmlNodeList list = doc.SelectNodes("files/file");

On Sun, 18 Feb 2007 12:54:13 +0100, Alan T <al*************@yahoo.com.au>
wrote:
I used
XmlDocument to load from a file.

Then how do I append an element to the end?

<files>
<file>
<name>graphic.txt</name>
<location>c:\temp</location>
</file>
</files>

I want to add this:
<file>
<name>editor.exe</name>
<location>c:\executable</location>
</file>


--
Happy coding!
Morten Wennevik [C# MVP]
Feb 18 '07 #3
Morten Wennevik <Mo************@hotmail.comwrote:
The code below assumes xml contains the original xml data and will add a node under the files node
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode fileNode = doc.CreateNode(XmlNodeType.Element, "file", doc.NamespaceURI);
XmlNode nameNode = doc.CreateNode(XmlNodeType.Element, "name", doc.NamespaceURI);
XmlNode locationNode = doc.CreateNode(XmlNodeType.Element, "location", doc.NamespaceURI);
Any reason you'd use XmlDocument.CreateNode rather than XmlDocument.CreateElement?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Feb 19 '07 #4
Probably because I didn't think of it :P
On Mon, 19 Feb 2007 08:58:30 +0100, Jon Skeet [C# MVP] <sk***@pobox.com>wrote:
Morten Wennevik <Mo************@hotmail.comwrote:
>The code below assumes xml contains the original xml data and will add a node under the files node
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);

XmlNode fileNode = doc.CreateNode(XmlNodeType.Element, "file", doc.NamespaceURI);
XmlNode nameNode = doc.CreateNode(XmlNodeType.Element, "name", doc.NamespaceURI);
XmlNode locationNode = doc.CreateNode(XmlNodeType.Element, "location", doc.NamespaceURI);

Any reason you'd use XmlDocument.CreateNode rather than XmlDocument.CreateElement?


--
Happy coding!
Morten Wennevik [C# MVP]
Feb 19 '07 #5

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

Similar topics

8
by: Wolfgang Lipp | last post by:
<annotation> the first eleven contributions in this thread started as an off-list email discussion; i have posted them here with the consent of their authors. -- _w.lipp </annotation> From:...
2
by: Wolfgang | last post by:
I'm applying a simple XSLT style sheet to an XML file. The style sheet is: http://piru.alexandria.ucsb.edu/~rnott/MetadataMapping/access-report.xsl The XML file is here: ...
10
by: Thomas Mlynarczyk | last post by:
Hi, Can I style the root element (HTML) just like any other (and will it work as expected) or are there restrictions to take into account? Thanks in advance, Thomas
26
by: Peter Olcott | last post by:
// // Is there something wrong with my syntax for the // Copy Constructor of an Array Element, or does // the C++ language not support this? // #include <stdio.h> #include <stdlib.h> ...
2
by: Jim Witte | last post by:
Hi, How do I set a file input element? If it's a text input, I can set the value property. But this doesn't work with file inputs. And no, I can't change it - I'm writing a script to...
1
by: DaaaDaaa | last post by:
Hi, I have a template that has several tables, I need to dynamically add new elements to one of them, however, I'm having a hard time to append a new element and some space like <br> after that....
6
by: Martin Plantec | last post by:
Hi again, If I may, I have another, slightly more complex, XSLT question. (I'm learning, but it's not as easy as I would have thought!) I would like a rule that says: for every element that has...
8
by: VK | last post by:
Can be multiple instances of element used as the root element? That's a curly way of asking, but I did not come up with a better sentence, sorry. What I mean is with a document like: <?xml...
0
by: joeller | last post by:
On October 13, 2006 Mark Rae wrote Hi, Firstly, I have not the slightest intention of using framesets - the reason for this post is merely to ask for assistance in furthering my understanding of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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
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,...

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.