C# xml配置文件的操作
xml文件是配置文件的常用格式,C#中操作常用Linq方式,本文簡單介紹其用法。
首先需要包含Linq,
using System.Xml.Linq;
如項目中的xml文件格式如下,
<?xml version="1.0" encoding="utf-8"?>
<channelconfig Company="zkmrobot" Board="usb3210">
<channel Type="Force" CHSN="F1-01" SN="080401" Name="CH01">
<x0>0.602</x0>
<y0>50.00</y0>
<x1>3.680</x1>
<y1>300.00</y1>
<offset>0.00</offset>
</channel>
</channelconfig>
創建對應的操作類
class ChannelConfig
{
private string channelName;
private double x0;
private double y0;
private double x1;
private double y1;
private double offset;
public ChannelConfig()
{ }
public string ChannelName
{
get { return channelName; }
set { channelName = value; }
}
public double X0
{
get { return x0; }
set { x0 = value; }
}
public double Y0
{
get { return y0; }
set { y0 = value; }
}
public double X1
{
get { return x1; }
set { x1 = value; }
}
public double Y1
{
get { return y1; }
set { y1 = value; }
}
public double Offset
{
get { return offset; }
set { offset = value; }
}
}
通道參數配置界面如下,
僅需要加一個DataGridView控件與兩個按鈕控件。
DataGridView控件用來顯示與操作xml中的文件,中間的橋梁就是上面設計的配置類。
具體動作由兩個按鈕來完成。代碼貼在窗口下面
public partial class Form3 : Form
{
List <ChannelConfig> ChannelconfigList = new List<ChannelConfig>();
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
buttonReadAll_Click(sender,e);
}
private void buttonReadAll_Click(object sender, EventArgs e)
{
//XmlDocumentReadAll();
LinqReadAll();
}
private void LinqReadAll()
{
XElement xe = XElement.Load(@"..\..\channel.xml");
IEnumerable<XElement> elements = from ele in xe.Elements("channel") select ele;
showInfoByElements(elements);
}
private void showInfoByElements(IEnumerable<XElement> elements)
{
foreach (var ele in elements)
{
ChannelConfig channelConfig = new ChannelConfig();
channelConfig.X0 = Convert.ToDouble(ele.Element("x0").Value);
channelConfig.Y0 = Convert.ToDouble(ele.Element("y0").Value);
channelConfig.X1 = Convert.ToDouble(ele.Element("x1").Value);
channelConfig.Y1 = Convert.ToDouble(ele.Element("y1").Value);
channelConfig.Offset = Convert.ToDouble(ele.Element("offset").Value);
channelConfig.ChannelName = ele.Attribute("Name").Value;
ChannelconfigList.Add(channelConfig);
}
dgvChannelConfig.GridColor = Color.Blue;
dgvChannelConfig.DataSource = ChannelconfigList;
}
private void XmlDocumentReadAll()
{
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.Load(@"..\..\channel.xml");
}
catch (Exception ep)
{
MessageBox.Show(ep.ToString());
}
//得到根節點channelconfig
XmlNode xn = xmlDoc.SelectSingleNode("channelconfig");
//得到根節點的所有子節點
XmlNodeList xnl = xn.ChildNodes;
foreach (XmlNode xn1 in xnl)
{
ChannelConfig channelConfig = new ChannelConfig();
// 將節點轉換為元素,便於得到節點的屬性值
XmlElement xe = (XmlElement)xn1;
//得到Name屬性的屬性值
channelConfig.ChannelName = xe.GetAttribute("Name").ToString();
// 得到channel節點的所有子節點
XmlNodeList xnl0 = xe.ChildNodes;
channelConfig.X0 = Convert.ToDouble(xnl0.Item(0).InnerText);
channelConfig.Y0 = Convert.ToDouble(xnl0.Item(1).InnerText);
channelConfig.X1 = Convert.ToDouble(xnl0.Item(2).InnerText);
channelConfig.Y1 = Convert.ToDouble(xnl0.Item(3).InnerText);
channelConfig.Offset = Convert.ToDouble(xnl0.Item(4).InnerText);
ChannelconfigList.Add(channelConfig);
}
dgvChannelConfig.GridColor = Color.Blue;
dgvChannelConfig.DataSource = ChannelconfigList;
}
private void buttonSaveCurrent_Click(object sender, EventArgs e)
{
XElement xe = XElement.Load(@"..\..\channel.xml");
if (dgvChannelConfig.CurrentRow != null)
{
//dgvChannelConfig.CurrentRow.Cells[0]對應著CHANNEL Name
string id = dgvChannelConfig.CurrentRow.Cells[0].Value.ToString();
IEnumerable<XElement> element = from ele in xe.Elements("channel")
where ele.Attribute("Name").Value == id
select ele;
if (element.Count() > 0)
{
XElement first = element.First();
//設置新的屬性
first.SetAttributeValue("Name", dgvChannelConfig.CurrentRow.Cells[0].Value.ToString());
//替換新的節點
first.ReplaceNodes(
new XElement("x0", dgvChannelConfig.CurrentRow.Cells[1].Value.ToString()),
new XElement("y0", dgvChannelConfig.CurrentRow.Cells[2].Value.ToString()),
new XElement("x1", dgvChannelConfig.CurrentRow.Cells[3].Value.ToString()),
new XElement("y1", dgvChannelConfig.CurrentRow.Cells[4].Value.ToString()),
new XElement("offset", dgvChannelConfig.CurrentRow.Cells[5].Value.ToString())
);
}
xe.Save(@"..\..\channel.xml");
buttonReadAll_Click(sender, e);
}
}
}
C# xml配置文件的操作