简体   繁体   English

在文档中的任何位置从另一个xml文件插入节点(C#)

[英]inserting nodes from another xml file at any place in a document(c#)

XmlTextReader myReader = new XmlTextReader("abc.xml");
XmlDocument mySourceDoc = new XmlDocument();
mySourceDoc.Load(myReader);
myReader.Close();

myReader = new XmlTextReader("pqr.xml");
XmlDocument myDestDoc = new XmlDocument();
myDestDoc.Load(myReader);
myReader.Close();

XmlNode rootDest = myDestDoc["root node"];
XmlElement nodeOrig = (XmlElement)mySourceDoc["root node"].ChildNodes[0];
XmlNode nodeDest = myDestDoc.ImportNode(nodeOrig, true);

try
{
    rootDest.AppendChild(nodeDest);
}
catch (Exception ex)
{
    MessageBox.Show("" + ex);
}
XmlTextWriter myWriter = new XmlTextWriter("pqr.xml", Encoding.UTF8);
myWriter.Formatting = Formatting.Indented;

myDestDoc.WriteTo(myWriter);
myWriter.Close();

the above code works well if nodes have to be inserted as children of root node.But i want to insert nodes as children of any other node.what should i do? 如果必须将节点作为根节点的子节点插入,则上面的代码效果很好。但是我想将节点作为其他任何节点的子节点插入。我该怎么办?

Say, you have the following xml: 说,您具有以下xml:

<CONFIGURATION>
  <File>D:\Test\TestFolder\TestFolder1\TestFile.txt</File>
  <Dir>D:\Test\TestFolder\TestFolder1\</Dir>
  <File>D:\Test\TestFolder\TestFolder2\TestFile02.txt</File>
  <File>D:\Test\TestFolder\TestFolder2\TestFile04.txt</File>
  <Dir>D:\Test\TestFolder\TestFolder2\</Dir>
</CONFIGURATION>

And you want to create a new one, containg only File nodes; 您想创建一个新文件 ,只包含File节点; also the root node must be called Files . 根节点也必须称为Files Such xml would look like this: 这样的xml看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<Files>
  <File>D:\Test\TestFolder\TestFolder1\TestFile.txt</File>
  <File>D:\Test\TestFolder\TestFolder2\TestFile02.txt</File>
  <File>D:\Test\TestFolder\TestFolder2\TestFile04.txt</File>
</Files>

The following code does this: 下面的代码执行此操作:

XElement infile = XElement.Load("In.xml");
XElement outfile = new XElement("Files");
foreach (XElement x in infile.Elements("File"))
    outfile.Add(x);
outfile.Save("Out.xml");    

This is a simple example, how you can transfer nodes from one xml document to another with LINQ To XML. 这是一个简单的示例,如何使用LINQ To XML将节点从一个xml文档转移到另一个。 Consider using this. 考虑使用此。

You should like it. 你应该喜欢它。

EDIT 编辑

Original xml: 原始xml:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <abc>
    <pdf>hhh</pdf>
  </abc>
</root>

User input: 用户输入:

abc#uuu#ttt

Output xml: 输出xml:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <abc>
    <pdf>hhh</pdf>
    <uuu>ttt</uuu>
  </abc>
</root>

The code: 编码:

        string userinput = "abc#uuu#ttt";
        XElement infile = XElement.Load("In.xml");
        XElement temp = infile;
        string[] commands = userinput.Split(new string[] { "#" }, StringSplitOptions.RemoveEmptyEntries);
        for (int i = 0; i < commands.Length; i++)
        {
            if (i + 1 == commands.Length)
                temp.Value = commands[i];
            else
            {
                if (temp.Element(commands[i]) == null)
                    temp.Add(new XElement(commands[i]));
                temp = temp.Element(commands[i]);
            }
        }
        infile.Save("Out.xml");

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

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