Adding Elements to an Xml File in C#

Adding elements to an xml file in C#

You're close, but you want name to be an XAttribute rather than XElement:

 XDocument doc = XDocument.Load(spath); 
XElement root = new XElement("Snippet");
root.Add(new XAttribute("name", "name goes here"));
root.Add(new XElement("SnippetCode", "SnippetCode"));
doc.Element("Snippets").Add(root);
doc.Save(spath);

Adding elements to xml file in C# using XDocument

You have problems because sub is not root element of document. So, when you do

xDoc.Element("sub").Add(root);

Then xDoc.Element("sub") returns null. Then when you try to call method Add you have NullReferenceException.

I think you need to add new sub element to sublist element:

xDoc.Root.Element("sublist").Add(root);

Also I suggest to improve naming. If you are creating element sub, then call varibale sub instead of naming it root (that is very confusing). E.g.

XDocument xdoc = XDocument.Load(sourceFile);
var sub = new XElement("sub",
new XAttribute("a", a),
new XAttribute("b", b),
new XAttribute("c", c));

xdoc.Root.Element("sublist").Add(sub);
xdoc.Save(sourceFile);

Add element at specific position in XML-file C#

Try something like this, each Note element is replaced:

var query=from n in xml.Root.Descendants("Note")
select n;

foreach(var elem in query.ToList())
elem.ReplaceWith(new XElement("notedate", new XAttribute("date", "date here"), elem));

How to add new element below existing element using xml Document

InsertAfter must be called on the parent node (in your case "NonFuel").

nonFuel.InsertAfter(xmlRecordNo, dispute);

It may look a little confusing but it reads this way: you are asking the parent node (nonFuel) to add a new node (xmlRecordNo) after an existing one (dispute).

A complete example is here:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(@"<NonFuel><Desc>Non-Fuel</Desc><Description></Description><Quantity/><Amount/><Additional/><Dispute>0</Dispute></NonFuel>");

XmlNode nonFuel = xmlDoc.SelectSingleNode("//NonFuel");
XmlNode dispute = xmlDoc.SelectSingleNode("//Dispute");

XmlNode xmlRecordNo= xmlDoc.CreateNode(XmlNodeType.Element, "Records", null);
xmlRecordNo.InnerText = Guid.NewGuid().ToString();
nonFuel.InsertAfter(xmlRecordNo, dispute);

C#: How to add Elements to XML using c# & foreach loop

Create the element in the for loop. Here's some working code, using XPath.

foreach( XmlElement ndNode in xml.SelectNodes( "//Elements/Element")) {
XmlElement newelem = xml.CreateElement("Value0");
newelem.InnerText = "test";
ndNode.InsertBefore(newelem, ndNode.FirstChild);
}

C# how to add elements to a xml file

Try this code :

XmlDocument doc = new XmlDocument();
doc.Load(spath);
XmlNode snippet = doc.CreateNode(XmlNodeType.Element, "Snippet", null);

XmlAttribute att = doc.CreateAttribute("name");
att.Value = name.Text;
snippet.Attributes.Append(att);

XmlNode snippetCode = doc.CreateNode(XmlNodeType.Element, "SnippetCode", null);
snippetCode.InnerText = code.Text;

snippet.AppendChild(snippetCode);

doc.SelectSingleNode("//Snippets").AppendChild(snippet);

Dynamically add element to XML file

Alright, I managed to fix this one after hours and hours of staring at it!

Turns out I can just make a foreach() where I iterate through all the files I want to read from, create a TileMap object and add it to a List which I then write to XML after the foreach(). I've updated the OP with my new code.



Related Topics



Leave a reply



Submit