I have a very basic XML structure/file on disk which is something like:
kdkdkdk
I can't see a way to do what you're up to without reading the full file, but as a workaround maybe you could treat the file as a plain text ie without the root node (it will be invalid XML). Then, you could just append new nodes to the file as plain text. And, since your XML parsing process loads the entire file anyway, it can add the root node before treating it as XML.
Somehow, you'll have to read the XML file, since you cannot just add a node to the document.
An Xml file can have only one root, so the new node will have to be added after the last data element, but, before the root closing tag. Otherwise the xml is not valid.
Depends.
If you want to do it the "correct" way, you have to read the file and the XML in it. You can avoid loading it completely into memory by using an XmlReader class for example.
However, if you definitely absolutely know the text-layout and the encoding of the file, you can avoid reading and re-writing it completely by opening it as random-access file (FileStream), skip to the end (minus the "<root/>"), add the new entry there and write the "<root/>" again.
If by not loading XML, you mean not building a DOM tree, VTD-XML is the only API that allows you to cut paste split or modify incrementally. Furthermore, because VTD-XML is memory efficent, you won't have to worry about the size of XML document. A similar post in Java is at How to update large XML file. Notice that vtd-xml is available in Java, C# , C and C++.
just use
string s = File.ReadAllText(path)
s = s.replace("</root>", newnode + "</root>")
File.WriteAllText(s, path)
System.IO.FileInfo[] aryFi = di.GetFiles("*.xml");
foreach (System.IO.FileInfo fi in aryFi) {
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load(fi.FullName);
XmlNode refelem = xmlDocument.LastChild;
XmlNode newElem = xmlDocument.CreateNode("element", "something", "");
newElem.InnerText = "sometext";
xmlDocument.InsertAfter(newElem, refelem);
}
I believe opening and inserting a node would be best option. Either way you would need to use IO, why not do it proper way?
For single file
System.Xml.XmlDocument xmlDocument = new System.Xml.XmlDocument();
xmlDocument.Load("file path");
XmlNode refelem = xmlDocument.LastChild;
XmlNode newElem = xmlDocument.CreateNode("element", "something", "");
newElem.InnerText = "sometext";
xmlDocument.InsertAfter(newElem, refelem);