博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

C#中XML使用总结

Posted on 2009-11-27 14:05  追梦人2017  阅读(236)  评论(0编辑  收藏  举报

XML文件由于格式规范,读写简单,所以非常适宜保存一些程序的配置信息。

下面总结一下在C#中读写XML文件的方法,随时更新。

1、获取某个节点的值。

代码
XDocument xdoc = XDocument.Load(@"dataConfig.xml");
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));
            }