C#编写XML文件及属性添加

这一章主要是上一章拿到的dataGridView数据编写成XML文件
如果没看过上一篇博客那么请点这里

代码拿走不谢!

   private void button2_Click(object sender, EventArgs e)
        { 
            //分页去除重复 我主要为了分类  可不要
            List str = new List();
            for (int i = 0; i < dataGridView1.RowCount-1; i++)
            {
                str.Add(dataGridView1.Rows[i].Cells[0].Value.ToString());
            }
            for (int i = 0; i < str.Count - 1; i++)
            {
                for (int j = str.Count - 1; j > i; j--)
                {
                    if (str[i].Equals(str[j]))
                    {
                        str.Remove(str[j]);
                    }
                }
            }
            //开始编写xml文件
            XmlDocument xmlDocument = new XmlDocument();//新建一个XML“编辑器”
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();//打开一个保存对话框
            saveFileDialog1.Filter = "xml文件(*.xml)|*.xml";//设置允许打开的扩展名
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)//判断是否选择了一个文件路径
            {
                XmlElement xmlElement_ufinterface = xmlDocument.CreateElement("ufinterface");//创建一个根节点
                for (int k = 0; k < str.Count; k++)
                {
                    XmlElement voucher = xmlDocument.CreateElement("voucher");
                    XmlAttribute id = xmlDocument.CreateAttribute("id");
                    //id.InnerText = listString[k].ToString();
                    id.InnerText = str[k].ToString();
                    voucher.SetAttributeNode(id);
                    #region 根节点属性添加
                    XmlAttribute xelType = xmlDocument.CreateAttribute("roottag");
                    xelType.InnerText = "voucher";
                    xmlElement_ufinterface.SetAttributeNode(xelType);
                    XmlAttribute billtype = xmlDocument.CreateAttribute("billtype");
                    xmlElement_ufinterface.SetAttributeNode(billtype);
                    XmlAttribute docid = xmlDocument.CreateAttribute("docid");
                    docid.InnerText = "666";
                    xmlElement_ufinterface.SetAttributeNode(docid);
                    XmlAttribute receiver = xmlDocument.CreateAttribute("receiver");
                    xmlElement_ufinterface.SetAttributeNode(receiver);
                    XmlAttribute Sender = xmlDocument.CreateAttribute("sender");
                    xmlElement_ufinterface.SetAttributeNode(Sender);
                    XmlAttribute proc = xmlDocument.CreateAttribute("proc");
                    xmlElement_ufinterface.SetAttributeNode(proc);
                    XmlAttribute codeexchanged = xmlDocument.CreateAttribute("codeexchanged");
                    xmlElement_ufinterface.SetAttributeNode(codeexchanged);
                    XmlAttribute exportneedexch = xmlDocument.CreateAttribute("exportneedexch");
                    xmlElement_ufinterface.SetAttributeNode(exportneedexch);
                    XmlAttribute renewproofno = xmlDocument.CreateAttribute("renewproofno");
                    xmlElement_ufinterface.SetAttributeNode(renewproofno);
                    XmlAttribute version = xmlDocument.CreateAttribute("version");
                    version.InnerText = "2.0";
                    xmlElement_ufinterface.SetAttributeNode(version);
                    #endregion

                    #region 头部节点集合
                    XmlElement xmlElement_head = xmlDocument.CreateElement("voucher_head");//创建一个节点
                    XmlElement voucher_type = xmlDocument.CreateElement("voucher_type");
                    xmlElement_head.AppendChild(voucher_type);
                    XmlElement fiscal_year = xmlDocument.CreateElement("fiscal_year");
                    xmlElement_head.AppendChild(fiscal_year);
                    XmlElement accounting_period = xmlDocument.CreateElement("accounting_period");
                    xmlElement_head.AppendChild(accounting_period);
                    XmlElement voucher_id = xmlDocument.CreateElement("voucher_id");
                    xmlElement_head.AppendChild(voucher_id);
                    XmlElement attachment_number = xmlDocument.CreateElement("attachment_number");
                    xmlElement_head.AppendChild(attachment_number);
                    XmlElement date = xmlDocument.CreateElement("date");
                    date.InnerText = DateTime.Now.ToString();
                    xmlElement_head.AppendChild(date);
                    XmlElement enter = xmlDocument.CreateElement("enter");
                    xmlElement_head.AppendChild(enter);
                    XmlElement cashier = xmlDocument.CreateElement("cashier");
                    xmlElement_head.AppendChild(cashier);
                    XmlElement signature = xmlDocument.CreateElement("signature");
                    xmlElement_head.AppendChild(signature);
                    XmlElement checker = xmlDocument.CreateElement("checker");
                    xmlElement_head.AppendChild(checker);
                    XmlElement posting_person = xmlDocument.CreateElement("posting_person");
                    xmlElement_head.AppendChild(posting_person);
                    XmlElement posting_date = xmlDocument.CreateElement("posting_date");
                    xmlElement_head.AppendChild(posting_date);
                    XmlElement memo1 = xmlDocument.CreateElement("memo1");
                    xmlElement_head.AppendChild(memo1);
                    XmlElement memo2 = xmlDocument.CreateElement("memo2");
                    xmlElement_head.AppendChild(memo2);
                    XmlElement voucher_making_system = xmlDocument.CreateElement("voucher_making_system");
                    xmlElement_head.AppendChild(voucher_making_system);
                    XmlElement revokeflag = xmlDocument.CreateElement("revokeflag");
                    xmlElement_head.AppendChild(revokeflag);
                    XmlElement reserve1 = xmlDocument.CreateElement("reserve1");
                    xmlElement_head.AppendChild(reserve1);
                    XmlElement reserve2 = xmlDocument.CreateElement("reserve2");
                    xmlElement_head.AppendChild(reserve2);
                    #endregion

                    #region body节点集合及文本添加
                    XmlElement xmlElement_body = xmlDocument.CreateElement("voucher_body");//创建一个节点
                    int row = dataGridView1.Rows.Count;//得到总行数 
                    int cell = dataGridView1.Rows[1].Cells.Count;
                    for (int i = 0; i < row - 1; i++)//得到总行数并在之内循环    
                    {
                        if (dataGridView1.Rows[i].Cells[0].Value.ToString() ==str[k].ToString())
                        {
                            //同上,创建一个个节点,并且附到之下
                            XmlElement xmlElement_entry = xmlDocument.CreateElement("enter");
                            XmlElement xmlElement_voucher_id = xmlDocument.CreateElement("voucher_id");
                            xmlElement_entry.AppendChild(xmlElement_voucher_id);
                            XmlElement xmlElement_voucherdate = xmlDocument.CreateElement("voucherdate");
                            xmlElement_entry.AppendChild(xmlElement_voucherdate);
                            XmlElement xmlElement_entry_id = xmlDocument.CreateElement("entry_id");
                            xmlElement_entry.AppendChild(xmlElement_entry_id);
                            XmlElement xmlElement_abstract = xmlDocument.CreateElement("abstract");
                            xmlElement_entry.AppendChild(xmlElement_abstract);
                            XmlElement xmlElement_account_code = xmlDocument.CreateElement("account_code");
                            xmlElement_entry.AppendChild(xmlElement_account_code);
                            XmlElement xmlElement_moneyout = xmlDocument.CreateElement("moneyout");
                            xmlElement_entry.AppendChild(xmlElement_moneyout);
                            XmlElement xmlElement_moneyon = xmlDocument.CreateElement("moneyon");
                            xmlElement_entry.AppendChild(xmlElement_moneyon);
                            XmlElement xmlElement_ccus_id = xmlDocument.CreateElement("ccus_id");
                            xmlElement_entry.AppendChild(xmlElement_ccus_id);
                            XmlElement xmlElement_csup_id = xmlDocument.CreateElement("csup_id");
                            xmlElement_entry.AppendChild(xmlElement_csup_id);
                            XmlElement xmlElement_dept_id = xmlDocument.CreateElement("dept_id");
                            xmlElement_entry.AppendChild(xmlElement_dept_id);
                            XmlElement xmlElement_operator = xmlDocument.CreateElement("operator");
                            xmlElement_entry.AppendChild(xmlElement_operator);
                            xmlElement_voucher_id.InnerText = dataGridView1.Rows[i].Cells[0].Value.ToString();
                            xmlElement_voucherdate.InnerText = dataGridView1.Rows[i].Cells[1].Value.ToString();
                            xmlElement_entry_id.InnerText = dataGridView1.Rows[i].Cells[2].Value.ToString();
                            xmlElement_abstract.InnerText = dataGridView1.Rows[i].Cells[3].Value.ToString();
                            xmlElement_account_code.InnerText = dataGridView1.Rows[i].Cells[4].Value.ToString();
                            xmlElement_moneyout.InnerText = dataGridView1.Rows[i].Cells[5].Value.ToString();
                            xmlElement_moneyon.InnerText = dataGridView1.Rows[i].Cells[6].Value.ToString();
                            xmlElement_ccus_id.InnerText = dataGridView1.Rows[i].Cells[7].Value.ToString();
                            xmlElement_csup_id.InnerText = dataGridView1.Rows[i].Cells[8].Value.ToString();
                            xmlElement_dept_id.InnerText = dataGridView1.Rows[i].Cells[9].Value.ToString();
                            xmlElement_operator.InnerText = dataGridView1.Rows[i].Cells[10].Value.ToString();
                            xmlElement_body.AppendChild(xmlElement_entry);
                        }
                    }
                    #endregion

                    xmlElement_ufinterface.AppendChild(voucher);
                    voucher.AppendChild(xmlElement_head);//给根节点voucher添加子节点
                    voucher.AppendChild(xmlElement_body);
                }
                xmlDocument.AppendChild(xmlDocument.CreateXmlDeclaration("1.0", "utf-8", null));//编写文件头
                xmlDocument.AppendChild(xmlElement_ufinterface);//根节点放入文件中
                xmlDocument.Save(saveFileDialog1.FileName);//保存文件
                MessageBox.Show("XML文件已生成");
            }
        }
    }

C#编写XML文件及属性添加_第1张图片

你可能感兴趣的:(.net,c#)