我怎样才能从一个文档复制XML块其他? [英] How can I copy a block of XML from one document to the other?

查看:123
本文介绍了我怎样才能从一个文档复制XML块其他?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个dataGridViews的加载每一个XML文件,我已经说得那么你可以将每个网格之间掉落行。然而此刻,所有它的作用是从DataGridView中复制数据。这工作得很好,但是我需要所有的复制,是有关该行的XML的。

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. However at the moment, all that it does is copy the data from the 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:

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



所以,我需要复制该行的XML,用户已经选择并将其插入到中另一个(相同的格式)的XML文档。

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.

到目前为止,我有这样的:

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



它运行正常,但所发生的一切我,我得到一个添加到XML文件的底部。如何选择XML的整块?

It runs fine, but all that happens i I get a added to the bottom of the XML file. How can I select the whole block of XML?

非常感谢您的帮助!

推荐答案

假设你想要复制从text1.xml元素块text2.xml,你可以使用LINQ到XML,下面的例子假定从文本1复制的所有条目的文本2:

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:

  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");



但你也可以使用其中,方法过滤得到的xml的一部分,样本下面是一个使用索引列表过滤:

But you also can use Where method to filter to get part of xml, sample below is to filter using list of indices:

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

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

在这里,我想用插入到最后最后,但如果你关心排序,你可以使用LINQ xDoc2找到正确的位置,那么就插入。

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.

这篇关于我怎样才能从一个文档复制XML块其他?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆