简体   繁体   English

如何将XML块从一个文档复制到另一个文档?

[英]How can I copy a block of XML from one document to the other?

I have two dataGridViews that load up an XML file each, I have made it so that you can drag and drop rows in between each of the grids. 我有两个dataGridViews分别加载一个XML文件,我这样做了,以便您可以在每个网格之间拖放行。 However at the moment, all that it does is copy the data from the dataGridView. 但是,目前,它所做的只是从dataGridView复制数据。 This works fine however I need to copy all of the XML that is relevant to that row. 效果很好,但是我需要复制与该行相关的所有XML。

Here is the XML that I have to work with: 这是我必须使用的XML:

<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 of the row that the user has selected and insert it into the in the other (same format) XML document. 因此,我需要复制用户选择的行的XML,并将其插入到另一个(相同格式)XML文档中。

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 runs fine, but all that happens i I get a added to the bottom of the XML file. 它运行正常,但是所有发生的事情我都添加到了XML文件的底部。 How can I select the whole block of XML? 如何选择整个XML块?

Thanks a lot for your help! 非常感谢你的帮助!

Assume you want to copy block of elements from text1.xml to text2.xml, you can use LINQ to XML, example below assumes copying all entries from text1 to text 2: 假设您要将元素块从text1.xml复制到text2.xml,可以使用LINQ to XML,下面的示例假定将所有条目从text1复制到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 also can use Where method to filter to get part of xml, sample below is to filter using list of indices: 但是您也可以使用Where方法进行过滤以获取xml的一部分,以下示例是使用索引列表进行过滤的:

  var filterIndices = new[] {600, 601, 700, 705};

  var doc1Entries =
      xDoc1.Descendants("stentry")
           .Where(x =>         
               filterIndices.Contains(int.Parse(x.Element("index").Value)));

In here, I assume to insert to the last using Last , but if you care about ordering, you can use LINQ on xDoc2 to find correct position, then do insert. 在这里,我假设使用Last插入到最后Last ,但是如果您担心订购,可以在xDoc2上使用LINQ查找正确的位置,然后进行插入。

Every XmlNode has several methods (and XmlDocument is a child class of XmlNode), so you can use xDoc.SelectNodes() or xDoc.SelectSingleNode() to pick a specific node wherever in the document structure, store that node in an object (let's call it needleNode), and then do xDoc.InsertBefore(Copy, ref needleNode) or xDoc.InsertAfter(Copy, ref needleNode) . 每个XmlNode都有几种方法(而XmlDocument是XmlNode的子类),因此您可以使用xDoc.SelectNodes()xDoc.SelectSingleNode()来选择文档结构中任何位置的特定节点,然后将该节点存储在对象中(让我们称它为needleNode),然后执行xDoc.InsertBefore(Copy,ref needleNode)xDoc.InsertAfter(Copy,refneedleNode) Using these four functions you can insert the xml section to absolutely ANY part in the structure of the second xml. 使用这四个函数,您可以将xml部分插入到第二个xml结构的绝对任何部分。

If your control is databound, you don't need to add/remove rows to the DataGridView's rows collection (you actually can't do this). 如果控件是数据绑定的,则无需在DataGridView的rows集合中添加/删除行(实际上,您不能这样做)。 Instead, add them to the underlying datasource collection (the collection you are setting in to the DataSource property of the DataGridView). 而是将它们添加到基础数据源集合(您要在其中设置该集合的DataGridView的DataSource属性)。 Afterwards you'll need to refresh the view of both datagridviews to reflect the change. 之后,您将需要刷新两个datagridviews的视图以反映更改。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM