将两个不同格式的XML文件,进行节点对照,并生成一个用于对照功能的XML

前端之家收集整理的这篇文章主要介绍了将两个不同格式的XML文件,进行节点对照,并生成一个用于对照功能的XML前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

经常有的需求是,需要将一种格式的XML转换成另一种XML。如果要实现这个功能首先需要将两个不同XML手动建立节点对照关系。然后存成XML或者数据文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.Xml;
using System.Collections;
using System.Windows;
using System.IO;
namespace XML_Dom
{
    public partial class Form2 : Form
    {
        private XmlDocument document;//xml文档对象
        private XmlNode newNode;
        private XmlAttribute newAttri;
        private XmlElement newElement;
        string systemPath;
        int ncNum = 0;
        public Form2()
        {
            InitializeComponent();
            Form_Load();

        }

        private void Form2_Load(object sender,EventArgs e)
        {


        }


        //加载XML文件
        private void LoadStuXml(string xml)
        {
            document = new XmlDocument();
            document.Load(xml);//加载xml文档
        }
       


        ArrayList newList = new ArrayList();
        private void TraverseTextNodeInnerText(XmlNode node)
        {

            if (node.HasChildNodes)
            {
                XmlNodeList nodeList = node.ChildNodes;
                foreach (XmlNode nd in nodeList)
                {
                    //if (nd.NodeType == XmlNodeType.Text)
                    if (nd.NodeType == XmlNodeType.Element)//去掉注释节点
                    {
                        //newList.Add(nd.InnerText);//获得节点值
                        newList.Add(nd.Name);//获得节点名称
                    }
                    TraverseTextNodeInnerText(nd);
                }
            }
        }

        public void GetAllTextNodeInnerText(string node,int x,int y,string s)
        {
            //XmlDocument doc = XMLLoad();
            XmlNode nd = document.SelectSingleNode(node);
            TraverseTextNodeInnerText(nd);
            //return this._TextNodeInnerTexts.ToArray();
            //int x = 10;
            //int y = 0;
          
            if (s == "textBox")
            {
                ncNum = newList.Count;
                this.panel1.AutoScroll = true;
                for (int i = 0; i < newList.Count; i++)
                {

                    y += 40;


                    TextBox tb = new TextBox();
                    tb.Location = new Point(x,y);

                    tb.Name = "tb" + i.ToString();
                    tb.Text = newList[i].ToString();
                    //this.Controls.Add(tb);
                    this.panel1.Controls.Add(tb);

                    Label lb = new Label();
                    lb.Name="lb"+ i.ToString();
                    lb.Location = new Point(x-25,y);
                    lb.Text = i.ToString();
                    this.panel1.Controls.Add(lb);


                }
            }
            if (s == "dropdownlist")
            {
                this.panel2.AutoScroll = true;
                for (int i = 0; i < ncNum; i++)
                {
                    y += 40;
                    ComboBox cb = new ComboBox();
                    cb.Location = new Point(x,y);

                    cb.Name = "cb" + i.ToString();
                    for (int j = 0; j < newList.Count; j++)
                    {
                        cb.Items.Add(newList[j].ToString());
                    }
                    //this.Controls.Add(cb);
                    this.panel2.Controls.Add(cb);

                    Label lb = new Label();
                    lb.Name = "dlb" + i.ToString();
                    lb.Location = new Point(x - 25,y);
                    lb.Text = i.ToString();
                    this.panel2.Controls.Add(lb);
                }
            }

        }

        private void button1_Click(object sender,EventArgs e)
        {
            // Create an instance of the open file dialog Box.
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            // Set filter options and filter index.
            openFileDialog1.Filter = "Text Files (.xml)|*.xml|All Files (*.*)|*.*";
            openFileDialog1.FilterIndex = 1;

            openFileDialog1.Multiselect = true;



            // Process input if the user clicked OK.
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                newList.Clear();
                systemPath = openFileDialog1.FileName;
                tbPath.Text = systemPath;
             
                LoadStuXml(systemPath);//打开NC模板文档
                GetAllTextNodeInnerText("ufinterface",60,10,"dropdownlist");
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender,EventArgs e)
        {
            if (comboBox1.Text == "应付单")
           {
               checkFile("../DuizhaoXML/"+comboBox1.Text + "对照表" + ".xml");
               LoadStuXml("../DemoXML/nc.xml");//打开NC模板文档
               GetAllTextNodeInnerText("ufinterface",50,"textBox");
           }
           if (comboBox1.SelectedText.ToString() == "应收单")
           {

           }
        }

        private void btnSave_Click(object sender,EventArgs e)
        {
            createNewXML();
        }
        private void createNewXML()
        {
            //int txtNum = panel1.Controls.Count;//xml节点个数
            //for(int i=0;i<txtNum;i++)
            //{
            //  //string t= this.Controls["tb"+i].Text;
            //  if (panel1.Controls[i].Name == "tb" + i)
            //    {   
            //        //return i;
            //        string t = panel1.Controls[i].Text;

            //    }

            //}

            try
            {
                XmlTextWriter xmlWriter;
                string strFilename = "../duizhaoXML/" + comboBox1.Text + "对照表" + ".xml";

                xmlWriter = new XmlTextWriter(strFilename,Encoding.Default);//创建一个xml文档
                xmlWriter.Formatting = Formatting.Indented;
                xmlWriter.WriteStartDocument();

                xmlWriter.WriteStartElement("Node");

                //int txtNum = panel1.Controls.Count;//xml节点个数
                int txtNum = ncNum;
                for (int i = 0; i < txtNum; i++)
                {
                    //string t= this.Controls["tb"+i].Text;
                    //if (panel1.Controls["tb"+i].Name == "tb" + i.ToString())
                    //{
                       
                        string t = panel1.Controls["tb"+i].Text;//NC模板节点
                        xmlWriter.WriteStartElement(t);
                        xmlWriter.WriteString( panel2.Controls["cb"+i].Text);//外部系统对应节点
                        xmlWriter.WriteEndElement();
                    //}

                }

                xmlWriter.WriteEndElement();
                xmlWriter.Flush();   
                xmlWriter.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
           

            MessageBox.Show("创建成功!");
        }

        private void checkFile(string path)
        {
            FileInfo TheFile = new FileInfo(path);
            if (TheFile.Exists)
            {
                MessageBox.Show("对照模板已存在!");
            }

        }
        
    }
}

效果如下

猜你在找的XML相关文章