将一个xml文档复制到指定xml元素的另一个xml文档。 [英] copy one xml document to another xml document at specified xml element.

查看:176
本文介绍了将一个xml文档复制到指定xml元素的另一个xml文档。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想将一个xml文档对象复制到特定xml元素的另一个xml文档对象。我试过但不能这样做。



下面是示例代码:

Hi,
I wanted to copy one xml document object to another xml document object at specific xml element. I tried but not able to do so.

Below is the sample code:

public void GenerateFooterTablixcode(XmlDocument destDoc, XmlElement reportItems)
// reportItems is the xml element belongs to destDoc xml document
{
    XmlDocument copydoc = new XmlDocument();
     copydoc.Load(@"C:\Users\Documents\Visual Studio 2010\Projects\Report Project1\RdlGeneration\XMLFile1.rdl");

    XmlNode newBook = destDoc.ImportNode(copydoc.DocumentElement, true);
    destDoc.ImportNode(copydoc.DocumentElement, true);
    destDoc.DocumentElement.AppendChild(newBook);
}





使用上面的代码,copydoc对象在destDoc对象的末尾处理。

我想在destDoc的特定元素reportItems中复制..





任何人都可以帮忙。



With the above code copydoc object is coping at the end of destDoc object.
where I wanted to copy at the specific element reportItems of destDoc..


Can any one please help.

推荐答案

这取决于你想要达到的目标...



假设你有2个xml文件。你想在某些条件下加入基于数据的os。请参阅:

It depends on what you want to achieve...

Let's say you have 2 xml files. You want to join data based os some condition. Please see:
//the content of xml files
string s1 = @"<persons>
<person id='1'>
<name>Arahid</name>
</person>
<person id='2'>
<name>Belleamour</name>
</person>
</persons>";

string s2 = @"<dobs>
<dob id='1'>1977-01-01</dob>
<dob id='2'>1978-11-01</dob>
</dobs>";

//create documents  
XDocument xdoc1 = XDocument.Parse(s1);
XDocument xdoc2 = XDocument.Parse(s2);

//get nodes from 1. and 2. xml based on id, create new XElement
var nodes = from a in xdoc1.Descendants("person")
    join b in xdoc2.Descendants("dob") on (int)a.Attribute("id") equals (int)b.Attribute("id")
    select new XElement ("person", new XAttribute("id", (string)a.Attribute("id")),
        new XElement("name", (string)a.Element("name")),
        new XElement("dob", (string)b.Value)
    );

//create new document
XDocument xdoc3 = new XDocument();
//add root node and nodes created above
xdoc3.Add(new XElement("persons", nodes));
//xdoc3.Save(@"FullFileName.xml");





结果:



Result:

<persons>
  <person id="1">
    <name>Arahid</name>
    <dob>1977-01-01</dob>
  </person>
  <person id="2">
    <name>Belleamour</name>
    <dob>1978-11-01</dob>
  </person>
</persons>







如需了解更多信息,请参阅: Linq To Xml [ ^ ]


您可以尝试此解决方案。未经测试但希望将解决或帮助您解决问题

You can try this solution . Not tested but hope will solve or help you to solve your problem
public void GenerateFooterTablixcode(string sourceXMLPATH, string targetXMLPATH,string sourceNode,string targetNode)
    {
        XmlDocument sourceXML = new XmlDocument();
        XmlDocument targetXML = new XmlDocument();
        sourceXML.Load(sourceXMLPATH);
        targetXML.Load(targetXMLPATH);
        XmlNode sXN = sourceXML.SelectSingleNode(sourceNode); // sourceNode should be the node name with depth like "Root/child1/child2/sourceNode"
        XmlNode tXN = targetXML.SelectSingleNode(targetNode); // sourceNode should be the node name with depth like "Root/child1/child2/targetNode"
        sXN.InnerXml = tXN.InnerXml;
        sourceXML.Save(sourceXMLPATH);
    }


这篇关于将一个xml文档复制到指定xml元素的另一个xml文档。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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