简体   繁体   中英

How can i import MULTIPLE XmlNodes from one document to another?

i am trying to add XmlNodes from one XmlDocument to another as a new node. My main issue is that i can import only the First or Last child of the document but not the ones in between. all of the nodes i am trying to import have the same layout but i cant seem to create any iteration for importing them all since i can only select either the FirstChild or LastChild - Please note this is also across 2 forms.

I am new to Xml but do not want to re-write my whole Xml Document over again in an XDocument, any help would be greatly appreciated, Thanks.

Code Below:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace XmlCreator
{
    public partial class Form_Member : Form
    {
        string MPlayID, MNick, MName, MMail, MICQ, MRemark;
        public static XmlDocument xmlMembers = null;
        static XmlNode rootNode = null;

        public Form_Member()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (xmlMembers != null)
            {
                XmlNode member = xmlMembers.CreateElement("member");

                XmlAttribute attID = xmlMembers.CreateAttribute("id");
                attID.Value = MPlayID;
                member.Attributes.Append(attID);

                XmlAttribute attNick = xmlMembers.CreateAttribute("nick");
                attNick.Value = MNick;
                member.Attributes.Append(attNick);

                rootNode.AppendChild(member);

                XmlNode MNameNode = xmlMembers.CreateElement("name");
                MNameNode.InnerText = MName;
                member.AppendChild(MNameNode);

                XmlNode MMailNode = xmlMembers.CreateElement("email");
                MMailNode.InnerText = MMail;
                member.AppendChild(MMailNode);

                XmlNode MICQNode = xmlMembers.CreateElement("icq");
                MICQNode.InnerText = MICQ;
                member.AppendChild(MICQNode);

                XmlNode MRemarkNode = xmlMembers.CreateElement("remark");
                MRemarkNode.InnerText = MRemark;
                member.AppendChild(MRemarkNode);

                xmlMembers.Save("memberXML.xml");
                clearTextFields();
            }     
        }

        private void Form_Member_Load(object sender, EventArgs e)
        {
            xmlMembers = new XmlDocument();
            rootNode = xmlMembers.CreateElement("members");
            xmlMembers.AppendChild(rootNode);
        }
    }
}

This is the form of which the Xml file i am trying to import is being created from, i am trying to import this to another form with the following code on the form i am trying to import it to.

code below:

    XmlNode memberNode = xmlSquad.ImportNode(Form_Member.xmlMembers.DocumentElement.FirstChild, true);
    xmlSquad.DocumentElement.AppendChild(memberNode);

To conclude, it is importing the FirstChild, however, i am making more than 1 memberNode in the xmlMembers.xml file from the other form which i can't find a way of copying over.

Any help will be appreciated, Thank you.

You need the following extension method :

public static class XmlNodeExtensions
{
    /// <summary>
    /// Copy all child XmlNodes from the source to the destination.
    /// </summary>
    /// <param name="source">Copy children FROM this XmlNode</param>
    /// <param name="destination">Copy children TO this XmlNode</param>
    public static void CopyChildren(this XmlNode source, XmlNode destination)
    {
        if (source == null || destination == null)
            throw new ArgumentNullException();
        var doc = destination.OwnerDocument;
        if (doc == null)
            throw new InvalidOperationException("null document");
        // Clone the array to prevent infinite loops when the two nodes are from the same document.
        foreach (var child in source.ChildNodes.Cast<XmlNode>().ToArray())
        {
            var copy = doc.ImportNode(child, true);
            destination.AppendChild(copy);
        }
    }
}

You can then use it like:

Form_Member.xmlMembers.DocumentElement.CopyChildren(xmlSquad.DocumentElement);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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