How to Add a New Node to Xml

c# - Adding new parent node to existing Xml File

Expanding on the comment by @hotfix, rename your root element and when I ran your code it was working for me. See the XML and code I was using below.

XML:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<testsuites id="" name="Test Projekt test">
<node_order />
<details />
<testsuite id="" name="Test Suite 1">
<node_order />
<details />
<testsuite id="" name="Test Suite Operation 2">
<node_order />
<details />
</testsuite>
</testsuite>
<testsuite id="" name="">
<node_order />
<details />
</testsuite>
</testsuites>

Code:

XmlDocument document = new XmlDocument();
document.Load(@"your\xml\file");

XmlElement testsuite = document.CreateElement("testsuite");
XmlAttribute ID = document.CreateAttribute("id");
XmlAttribute Name = document.CreateAttribute("name");
XmlElement node_order = document.CreateElement("node_order");
XmlElement details = document.CreateElement("details");

document.DocumentElement.AppendChild(testsuite);
testsuite.Attributes.Append(ID);
testsuite.Attributes.Append(Name);
testsuite.AppendChild(node_order);
testsuite.AppendChild(details);

document.Save(@"your\xml\file");

Adding node to existing xml element

You want to use SelectSingleNode:

XmlNode xhousing = xDoc.SelectSingleNode(@"//house[@windowsc='three']/windows");
XmlNode xName = xDoc.CreateElement("Name");
xName.InnerText = "hi";
xhousing.AppendChild(xName);

C#, XML, adding new nodes

Your first problem is that the node names in your XPath don't match those of the XML. XML is case sensitive, so you need to use Root, not root:

XmlNode root = xmldoc.SelectSingleNode("/ns:Root/ns:profesori", nsMgr);

Next, instead of xmldoc.NamespaceURI, use the actual namespace uri:

string strNamespace= "http://prpa.org/XMLSchema1.xsd";
nsMgr.AddNamespace("ns", strNamespace);

or do this:

string strNamespace= xmldoc.DocumentElement.NamespaceURI;
nsMgr.AddNamespace("ns", strNamespace);

The NamespaceURI of an XmlDocument object will always be an empty string.

And you should also use this namespace when creating your elements:

XmlNode prof = xmldoc.CreateNode(XmlNodeType.Element, "profesor", strNamespace);

XmlNode ime = xmldoc.CreateNode(XmlNodeType.Element, "ime", strNamespace);
ime.InnerText = name;
prof.AppendChild(ime);

XmlNode prezime = xmldoc.CreateNode(XmlNodeType.Element, "prezime", strNamespace);
prezime.InnerText = surname;
prof.AppendChild(prezime);

root.AppendChild(prof);

You might also consider using the CreateElement() method, which would be slightly shorter:

XmlNode prof = xmldoc.CreateElement("profesor", strNamespace);

Or, my preference would be to use an XmlWriter:

using(XmlWriter writer = root.CreateNavigator().AppendChild())
{
writer.WriteStartElement("profesor", strNamespace);
writer.WriteElementString("ime", strNamespace, name);
writer.WriteElementString("prezime", strNamespace, surname);
writer.WriteEndElement();
}

inserting a node after a specific node in xml file

I haven´t fully tested this, but I´m almost sure this should do the trick:

            foreach (XmlNode node in xdoc.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']"))
{
XmlNodeList srcNodes = node.SelectNodes("/eventlist/event[@type='AUDIOPLAYER']");
foreach (XmlNode srcNode in srcNodes)
{

XmlNode newElem = xdoc.CreateElement("event");
XmlAttribute newAttr = xdoc.CreateAttribute("type");
newAttr.Value = "VIZ";
newElem.Attributes.Append(newAttr);
srcNode.ParentNode.InsertAfter(newElem, srcNode);
}
}

The problem is you were selecting single node from matching expression, and you need to select all the nodes that match that and insert the new node after each of them.

Hope this helps!

XML : Add Nodes in between the specific Node

I suggest you to use Append and not Add.. like in this example that i have found online:

' create new instance of XmlDocument
Dim doc As New XmlDocument()

' load from file
doc.Load(filename)

' create node and add value
Dim node As XmlNode = doc.CreateNode(XmlNodeType.Element, "FolderList", Nothing)
'node.InnerText = "this is new node";

' create title node
Dim nodeTitle As XmlNode = doc.CreateElement("FolderName")
'add value for it
nodeTitle.InnerText = "G:\Sony"

' create Url node
Dim nodeUrl As XmlNode = doc.CreateElement("Checked")
nodeUrl.InnerText = "True"

' add to parent node
node.AppendChild(nodeTitle)
node.AppendChild(nodeUrl)

' add to elements collection
doc.DocumentElement.AppendChild(node)

' save back
doc.Save(filename)


Related Topics



Leave a reply



Submit