Operation of the C # XML configuration file

Source: Internet
Author: User

The XML file is a common format for configuration files, and C # operates in common LINQ, and this article briefly describes its usage.

First, you need to include LINQ,

Using System.Xml.Linq;

As the XML file format in the project is as follows,

<?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>

Create a corresponding action class

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

}

The Channel parameter configuration interface is as follows,

You only need to add a DataGridView control with two button controls.

The DataGridView control is used to display and manipulate the files in the XML, and the Middle bridge is the configuration class designed above.

The specific action is done by two buttons. Code pasted underneath the window

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

Get root node Channelconfig
XmlNode xn = xmldoc.selectsinglenode ("Channelconfig");

Get all the child nodes of the root node
XmlNodeList xnl = xn. ChildNodes;

foreach (XmlNode xn1 in XNL)
{
Channelconfig channelconfig = new Channelconfig ();

Convert a node to an element to make it easier to get the attribute value of a node
XmlElement XE = (XmlElement) xn1;

Get the property value of the Name property
Channelconfig.channelname = Xe. GetAttribute ("Name"). ToString ();

Get all the child nodes of the channel node
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] corresponds to 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 ();
Set a new property
First. Setattributevalue ("Name", Dgvchannelconfig.currentrow.cells[0]. Value.tostring ());

Replace the new node
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);
}
}

}

Operation of the C # XML configuration file

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.