C# XmlDocument生成xml,并解决System.ArgumentException: 要插入的节点出自不同的文档上下文。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CreateXML
{
    class Program
    {
        static void Main(string[] args)
        {

            if (File.Exists("1.xml"))
            {
                File.Delete("1.xml");
            }
            XmlDocument xmlDoc = new XmlDocument();


            XmlNode xmlNode = xmlDoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            XmlNode myNode = xmlDoc.CreateNode(XmlNodeType.Element, "appSetting", "");


            xmlDoc.AppendChild(xmlNode);
            xmlDoc.AppendChild(myNode);


            for (int i = 0; i < 10; i++)
            {
                XmlElement xmlElement = xmlDoc.CreateElement("", "test" + i.ToString(), "");

                xmlElement.InnerText = "Visible";


                XmlAttribute xmlAttribute = xmlDoc.CreateAttribute("width");

                xmlAttribute.Value = "10";


                xmlElement.Attributes.Append(xmlAttribute);


                myNode.AppendChild(xmlElement);
            }

            xmlDoc.Save("1.xml");







//下面的代码是进行排序的。
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load("1.xml");

            File.Delete("1.xml");


            XmlDocument xmlDoc1 = new XmlDocument();


            XmlNode xmlNode1 = xmlDoc1.CreateNode(XmlNodeType.XmlDeclaration, "", "");
            XmlNode myNode1 = xmlDoc1.CreateNode(XmlNodeType.Element, "appSetting", "");


            xmlDoc1.AppendChild(xmlNode1);
            xmlDoc1.AppendChild(myNode1);





            XmlNode root = xmlDocument.SelectSingleNode("appSetting");

            Dictionary<string, string> keyValuePairs = new Dictionary<string, string>()
            {
                { "test3","test3"},
                { "test4","test4"},
                { "test5","test5"},
                { "test6","test6"},
                { "test7","test7"},
                { "test8","test8"},
                { "test9","test9"},
                { "test0","test0"},
                { "test1","test1"},
                { "test2","test2"},
            };


            XmlNodeList list = root.ChildNodes;

            var queue = new Queue<XmlNode>();

            foreach (KeyValuePair<string, string> item in keyValuePairs)
            {
                for (int i = 0; i < list.Count - 1; i++)
                {
                    if (list[i].Name == item.Value)
                    {
                        queue.Enqueue(list[i]);

                    }
                }
            }
            while (queue.Count != 0)
            {
                //rootNode2.AppendChild(queue.Dequeue());//错误代码:要插入的节点出自不同的文档上下文。
                myNode1.AppendChild(xmlDoc1.ImportNode(queue.Dequeue(), true));
            }
            xmlDoc1.Save("1.xml");


        }
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值