How can I copy an XML block from one document to another?

I have two dataGridViews that load an XML file each, I made it so that you can drag rows between each grid. However, for now, all he does is copy the data from the dataGridView. This works great, however I need to copy all the XML that is relevant to this line.

Here is the XML I have to work with:

<WindowBuilderProject> <stringtable> <stentry>0..607</stentry> //All of the other records <stentry> <index>608</index> <sid>MNUB_AUTO</sid> <val> <en>AUTO</en> </val> <params> <fontref>0</fontref> <numref>0</numref> <clip>FALSE</clip> <include>TRUE</include> <protected>FALSE</protected> <cwidth>-1</cwidth> <dwidth>0</dwidth> </params> </stentry> </stringtable> </WindowBuilderProject> 

So, I need to copy the XML string that the user selected and paste it into another (same format) XML document.

So far I have this:

 string location = "/WindowBuilderProject/stringtable/stentry[index='" + rowIndexOfItemUnderMouseToDrop + "']"; XmlNode Copy = xDoc.ImportNode(xDoc2.SelectSingleNode(location), false); xDoc.DocumentElement.AppendChild(Copy); //This is just supposed to add it to the end, I will worry about ordering once it works 

It works fine, but everything that happens is what I get at the bottom of the XML file. How can I highlight the whole XML block?

Thank you very much for your help!

+1
source share
3 answers

Suppose you want to copy a block of elements from text1.xml to text2.xml, you can use LINQ to XML, the example below involves copying all the entries from text1 to text 2:

  var xDoc1 = XDocument.Load("C:\\text1.xml"); var xDoc2 = XDocument.Load("C:\\text2.xml"); var doc1Entries = xDoc1.Descendants("stentry"); var cloneEntries = doc1Entries.Select(x => new XElement(x)); xDoc2.Descendants("stentry").Last().AddAfterSelf(cloneEntries); xDoc2.Save("C:\\text2.xml"); 

But you can also use the Where method for filtering to get the xml part, the sample below for filtering using a list of indexes:

  var filterIndices = new[] {600, 601, 700, 705}; var doc1Entries = xDoc1.Descendants("stentry") .Where(x => filterIndices.Contains(int.Parse(x.Element("index").Value))); 

Here, I assume that you need to paste last using Last , but if you want to arrange, you can use LINQ for xDoc2 to find the correct position, and then paste.

+4
source

Each XmlNode has several methods (and XmlDocument is a child of XmlNode), so you can use xDoc.SelectNodes () or xDoc.SelectSingleNode () to select a specific node, wherever it is in the document structure, save this node object in object (call it in needleNode), and then do xDoc.InsertBefore (Copy, ref needleNode) or xDoc.InsertAfter (Copy, ref needleNode) . Using these four functions, you can insert the xml section into absolutely ANY part in the structure of the second xml.

+2
source

If your control is a data binding, you do not need to add / remove rows to the DataGridView row collection (you really cannot do this). Instead, add them to the base data collection (the collection into which you set the DataSource DataGridView property). Then you will need to update the view of both datagridviews to reflect this change.

0
source

Source: https://habr.com/ru/post/1496748/


All Articles