XML文件做数据源的读取使用

Modelxml.xml内容

<?xml version="1.0" encoding="utf-8"?>

< models >

  <model id="zhangsan">

    <name>张三</ name >

    <sex>1</ sex >

    <age>24</age>

  </ model >

</ models >

 

读取特定对象

 private Model MessageLoad(){

Model model=new Model();

//CS程序

string smtpXmlPath = string.Format(@"{0}\manager\modelxml.xml",             Directory.GetParent(AppDomain.CurrentDomain.BaseDirectory).Parent.Parent.FullName);

//BS程序

string smtpXmlPath =string.Format(@"{0}\ manager\modelxml.xml", AppDomain.CurrentDomain.BaseDirectory);

 

XmlDocument userDocument = new XmlDocument();

userDocument.Load(EmailHelper.SmtpXmlPath);

XmlNode senderNodeList =userDocument.SelectSingleNode("/models/model[@id=\"zhangsan\"]");

            if (senderNodeList.HasChildNodes)

            {

              Model.Name = senderNodeList["name"].InnerText;

              Model.Sex = senderNodeList["sex"].InnerText;

              Model.Age = senderNodeList["age"].InnerText;

           

            }

  Return  model;

}

读取xml集合并绑定

public static void BindExperimentPath(DropDownList ddlModel)

        {

            DataSet ds = new DataSet();

            objDataSet.ReadXml(string.Format(@"{0}\models\ modelxml.xml", AppDomain.CurrentDomain.BaseDirectory));

            ddlModel.DataSource = ds;

            ddlModel.DataTextField = "name";

            ddlModel.DataValueField = "age";

            ddlModel.DataBind();

   ddlModel.Items.Insert(0, new ListItem("请选择", ""));

        }

 

posted @ 2011-08-24 09:34  Cynosure鱼  阅读(499)  评论(0)    收藏  举报