views:

51

answers:

3

So say I have one xml file such as this:

<shapes>
    <shape>shape1</shape>
</shapes>

And another xml file like this:

<parentNode>
    <shapes>
        <shape>shape 2</shape>
    </shapes>
</parentnode>

I would like the output to be:

<parentNode>
    <shapes>
        <shape>shape1</shape>
        <shape>shape 2</shape>
    </shapes>
</parentnode>

The context is that I am using the visio schema but I wish the config file for an application which writes visio xml files to be a stripped down version of a visio config file. It should allow users to change shape properties, e.g. "process" to have a yellow colour AND it should allow them to add new shapes for example "AccountsTable" which the application will search for before using a standard shape and use the custom shape instead in some circumstances.

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

What should I be looking at to achieve this? The dataset method is pretty useless.

Many thanks!!!

A: 

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.

wllmsaccnt
Sorry, to be clear there is no assumed knowledge of any of the node names. So what I mean regarding the leaf nodes is if there are two branches in both trees with identical nodes apart from the leaves then the leaves are added to one branch (but nothing is replaced).
Thomas King
I see. How large are the xml documents? Would it affect performance to use a DOM style parser?
wllmsaccnt
A: 

You can load both files into two XElement objects, locate the target nodes in both objects and add or remove as you wish.

Here is a sample:

var doc1 = XDocument.Parse(file1).Element("shapes");
var doc2 = XDocument.Parse(file2).Element("parentNode").Element("shapes");

doc2.Add(doc1.Nodes());
Y Low
A: 

I don't think there is an easy solution. Considering that you are not restricted to merging the contents of the Shapes node, i think you will have to parse through the nodes of one of the document recursively, checking whether each of these nodes is present in the other document through XPath. And once you find a node that is common in both the documents, you can merge the contents of one in the other. It is hardly efficient and there may be a better way but thats the best I can think of.

shake