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);
}
}
}