读取xml

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;

namespace XML读取
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //3.5下使用此类,也可以使用XmlDocument类来处理xml数据
            XDocument doc = XDocument.Load(@"C:\App.config");
            //注意这里必须使用XDocument.Root的属性,首先获取根元素
            //然后使用根元素获取指定标签名称的元素
            foreach (XElement ele in doc.Root.Elements("connectionStrings"))
            {
                //Console.WriteLine(ele.Name);
                foreach (XElement eleChild in ele.Elements("add"))
                {
                    Console.WriteLine(eleChild.Attribute("connectionString").Value);
                }
            }
        }
        /*
         * app.config
        <?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add connectionString="2012-2003"/>
  </connectionStrings>
</configuration>
        */
    }
}

posted @ 2013-02-21 21:05  feidaochuanqing  阅读(147)  评论(0编辑  收藏  举报