What is the fastest way to combine two xml files into one

后端 未结 11 1565
广开言路
广开言路 2020-11-29 01:43

If I have two string of xml1 and xml2 which both represent xml in the same format. What is the fastest way to combine these together? The format is not important, but I ju

11条回答
  •  感动是毒
    2020-11-29 02:23

    If you can guarantee this format you can combine them by doing string manipulation:

    • Read the first file, keep everything before ""
    • Read the second file, remove the part up to ""
    • Combine those strings.

    This should be the fastest way since no parsing is needed.

    const string RelevantTag = "AllNodes";
    
    string xml1 = File.ReadAllText(xmlFile1);
    xml1 = xml1.Substring(0, xml.LastIndexOf(""));
    
    string xml2 = File.ReadAllText(xmlFile2);
    xml2 = xml2.Substring(xml.IndexOf("<" + RelevantTag + ">") + "<" + RelevantTag + ">".Length, xml1.Length);
    
    File.WriteAllText(xmlFileCombined, xm1 + xml2);
    

    That said I would always prefer the safe way to the fast way.

提交回复
热议问题