Merging two xml files in C# without appending and without deleting anything (example given)

后端 未结 3 580
野的像风
野的像风 2021-01-23 07:55

So say I have one xml file such as this:


    shape1

And another xml file like this:

3条回答
  •  南笙
    南笙 (楼主)
    2021-01-23 08:23

    Psuedo code, I am guessing at the method names.

    ...
    xmlreader xmlToMerge1 = xmlreader.create(XmlSourceVariableHere);
    xmlreader xmlToMerge2 = xmlreader.create(XmlSourceVariableToMergeHere);
    xmlwriter xmlout = new xmlwriter(someStreamOrOther);
    
    xmlout.writeBeginElement("parentnode");
    xmlout.writeBeginElement("shapes");
    
    while (xmlToMerge1.Read())
     {
     if (xmlreader.nodetype == element && xml.Name == "shape")
      {
      xmlToMerge1.WriteNodeTo(xmlout);
      }
     }
    
    while (xmlToMerge2.Read())
     {
     if (xmlToMerge2.nodetype == element && xmlToMerge2.Name == "shape")
      {
      xmlToMerge2.WriteNodeTo(xmlout);
      }
     }
    
    
    xmlout.writeEndNode(); // end shapes
    xmlout.writeEndNode(); // end parentnode
    

    I remember that there is a command to write a node from a reader to a writer, but I don't remember what it is specifically, you'll have to look that one up.

    What exactly do you mean by the following?

    In terms of the merge it basically needs to stick the right leaf nodes in the right places if that makes sense? Without overwriting anything unless the config file has been explicitly written to do so, e.g. a custom "shape 2".

    You'll have to explain your requirements a bit more if you want an answer to be more detailed than simply merging nodes.

提交回复
热议问题