C# 写XML文件
/// <summary>x
/// 修改xml文件
/// </summary>
/// <param name="dt"></param>
private void ModifyXml(DataTable dt)
{
XmlDataDocument doc = new XmlDataDocument();
try
{
doc.Load(Xml);
XmlNode dataTableSettingsNode = doc.SelectSingleNode("DataTable_Settings");//根结点
XmlNode dataCellsNode = dataTableSettingsNode.SelectSingleNode("data_cells");
if (null == dataCellsNode) return;
XmlNodeList dataCellNode = dataCellsNode.SelectNodes("data_cell");
foreach (XmlNode node in dataCellNode)
{
var chinesename = node.SelectSingleNode("chinese_name").InnerText;
var row = SearchRows(chinesename, dt);
node.SelectSingleNode("show_index").InnerText = (int.Parse(row[0].ToString()) + 1).ToString();
node.SelectSingleNode("visible").InnerText = row[1].ToString();
node.SelectSingleNode("width").InnerText = row[3].ToString();
switch (row[4].ToString())
{
case "左对齐":
node.SelectSingleNode("align").InnerText = "1";
break;
case "右对齐":
node.SelectSingleNode("align").InnerText = "2";
break;
case "居中对齐":
node.SelectSingleNode("align").InnerText = "0";
break;
default: break;
}
}
doc.Save(Xml);
}
catch (Exception ex)
{
}
}