zoukankan      html  css  js  c++  java
  • 将两个不同格式的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, 10, "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("对照模板已存在!");
                }
    
            }
            
        }
    }
    

    效果如下


  • 相关阅读:
    VS2012的恢复默认窗口的基本常用设置
    在 CentOS 6.4 上安装 CloudStack 4.2
    centos7上安装mysql
    myeclipse安装aptana插件
    访问WEB-INF下的JSP (转载)
    通过字节流复制大文件内容到指定的文件
    java 通过bufferedReader和bufferedWriter 拷贝文件
    Date类获取日期的方法失效的解决办法
    java中string 类型的对象间比较的学习笔记
    ubuntu下搭建nfs服务器
  • 原文地址:https://www.cnblogs.com/suixufeng/p/3336071.html
Copyright © 2011-2022 走看看