c# 操作xml

1 篇文章 0 订阅

casttype.xml:

<?xml version="1.0" encoding="utf-8"?> 小明 000205 小红 000026440304 黎明 000002320500 古天乐 000002320506

C# 程序:

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

   public List<string> listCaseType = new List<string>();
    private void btn_getXML_Click(object sender, EventArgs e)
    {
       
            DirectoryInfo dir = new DirectoryInfo(Application.StartupPath).Parent.Parent;
            string target = dir.FullName;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(target + "\\FormMainBusiness\\CastType.xml");
            string xmlData = xmlDoc.InnerXml;
            txt_xml.Text = formatXml(xmlData);
       
    }

    private void btn_SaveXml_Click(object sender, EventArgs e)
    {
        #region  (待定)检查xml的格式是否正确
        //System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
        //doc.LoadXml(txt_xml.Text.ToString());
        #endregion

        DirectoryInfo dir = new DirectoryInfo(Application.StartupPath).Parent.Parent;
        string target = dir.FullName;
        if (rB_CaseType.Checked == true)
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(txt_xml.Text.ToString());
            doc.Save(target + "\\FormMainBusiness\\CastType.xml");
        }
        else
        {
            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.LoadXml(txt_xml.Text.ToString());
            doc.Save(target + "\\FormMainBusiness\\PolicyPlan.xml");
        }
        MessageBox.Show("保存成功");
        DialogResult = DialogResult.OK;

    }

    #region  处理xml工具
    private static string formatXml(object xml)
    {
        XmlDocument xd;
        if (xml is XmlDocument)
        {
            xd = xml as XmlDocument;
        }
        else
        {
            xd = new XmlDocument();
            xd.LoadXml(xml as string);
        }
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        XmlTextWriter xtw = null;
        try
        {
            xtw = new XmlTextWriter(sw);
            xtw.Formatting = Formatting.Indented;
            xtw.Indentation = 1;
            xtw.IndentChar = '\t';
            xd.WriteTo(xtw);
        }
        finally
        {
            if (xtw != null)
                xtw.Close();
        }
        return sb.ToString().Replace("{", "").Replace("}", "");
    }
    public string DataTableToXml(DataTable dt)
    {
        StringBuilder strXml = new StringBuilder();
        strXml.AppendLine("<?xml version=" + "\"1.0\"" + " encoding=" + "\"utf-8\"" + "?>");
        strXml.AppendLine("<XmlTable>");
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            strXml.AppendLine("<rows>");
            for (int j = 0; j < dt.Columns.Count; j++)
            {
                strXml.AppendLine("<" + dt.Columns[j].ColumnName + ">" + dt.Rows[i][j] + "</" + dt.Columns[j].ColumnName + ">");
            }
            strXml.AppendLine("</rows>");
        }
        strXml.AppendLine("</XmlTable>");
        return strXml.ToString().Replace("{", "").Replace("}", "");
    }
    private DataSet ConvertXMLToDataSet(string xmlData)
    {
        StringReader stream = null;
        XmlTextReader reader = null;
        try
        {
            DataSet xmlDS = new DataSet();
            stream = new StringReader(xmlData);
            reader = new XmlTextReader(stream);
            xmlDS.ReadXml(reader);
            return xmlDS;
        }
        catch (Exception ex)
        {
            string strTest = ex.Message;
            return null;
        }
        finally
        {
            if (reader != null)
                reader.Close();
        }
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值