In C#, is there a way to add an XML node to a file on disk WITHOUT loading it first?

前端 未结 8 1843
后悔当初
后悔当初 2020-12-20 14:14

I have a very basic XML structure/file on disk which is something like:


    kdkdkdk

        
相关标签:
8条回答
  • 2020-12-20 14:41

    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.

    0 讨论(0)
  • 2020-12-20 14:42

    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.

    0 讨论(0)
  • 2020-12-20 14:42

    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.

    0 讨论(0)
  • 2020-12-20 14:46

    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++.

    0 讨论(0)
  • 2020-12-20 14:48

    just use

    string s = File.ReadAllText(path)
    s = s.replace("</root>", newnode + "</root>")
    File.WriteAllText(s, path)
    
    0 讨论(0)
  • 2020-12-20 14:55
    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);
    
    0 讨论(0)
提交回复
热议问题