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