XML element addition

  • Thread starter Thread starter Alan T
  • Start date Start date
A

Alan T

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>
 
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");
 
Thanks alot, it works very well.

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");
 
Morten Wennevik said:
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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top