Fastest way to add new node to end of an xml?

前端 未结 10 678
深忆病人
深忆病人 2020-11-29 10:21

I have a large xml file (approx. 10 MB) in following simple structure:


   .......
   .......         


        
10条回答
  •  春和景丽
    2020-11-29 11:18

    I attempted to use code other answers had suggested but ran into an issue where sometimes calling .length on my strings was not the same as the number of bytes for the string so I was inconsistently losing characters. I modified it to get the byte count instead.

    var endTag = "";
    var nodeText = GetNodeText();
    using (FileStream file = File.Open("my.log", FileMode.Open, FileAccess.ReadWrite))
    {
        file.BaseStream.Seek(-(Encoding.UTF8.GetByteCount(endTag)), SeekOrigin.End);
        fileStream.Write(Encoding.UTF8.GetBytes(nodeText), 0, Encoding.UTF8.GetByteCount(nodeText));
        fileStream.Write(Encoding.UTF8.GetBytes(endTag), 0, Encoding.UTF8.GetByteCount(endTag));
    }
    

提交回复
热议问题