XML文件由于格式规范,读写简单,所以非常适宜保存一些程序的配置信息。
下面总结一下在C#中读写XML文件的方法,随时更新。
1、获取某个节点的值。
代码
XDocument xdoc = XDocument.Load(@"dataConfig.xml");
this.TB_MMZS.Text = xdoc.Element("Cost").Element("BookInfo").Element("PageWordCount").Value;
this.TB_MMZS.Text = xdoc.Element("Cost").Element("BookInfo").Element("PageWordCount").Value;
2、查询XML文档。
代码
var query = from bookformat in xdoc.Element("Cost").Element("BookInfo").Element("BookFormat").Descendants("BookFormatItem")
select bookformat;
foreach (var item in query)
{
this.DDL_KB.Items.Add(new ListItem(item.Element("Text").Value,item.Element("Value").Value));
}
select bookformat;
foreach (var item in query)
{
this.DDL_KB.Items.Add(new ListItem(item.Element("Text").Value,item.Element("Value").Value));
}