将数据绑定到xml的下拉列表中

问题描述:

我有一个页面,我想读取xmlfile并将其数据绑定到下拉列表。

我能够读取xml文件但我如何绑定数据。

我的xml文件就像是



< field name =abcop =<,>,%value =1 >

< field name =xyzop =<,>,%value =2>



i想要在一个下拉列表中绑定字段名称,在另一个下拉列表中绑定。

I have a page where i want to read the xmlfile and bind the data of it to a dropdownlist .
I am able to read the xml file but how i bind the data .
My xml file is like

<field name="abc" op="<,>,%" value="1">
<field name="xyz" op="<,>,%" value="2">

i want to bind the field name in one dropdown and op in other .

试试这个:

Try this:
public DataTable fnXMLToDataTable(string filePath)
{
    //create the DataTable that will hold the data
    DataTable table = new DataTable("XmlData");
    try
    {
        //open the file using a Stream
        using(Stream stream = new  FileStream(filePath, FileMode.Open, FileAccess.Read))
        {
            //create the table with the appropriate column names
            table.Columns.Add("Name", typeof(string));
            table.Columns.Add("Code", typeof(int));

            //use ReadXml to read the XML stream
            table.ReadXml(stream);

            //return the results
            return table;
        }                
    }
    catch (Exception ex)
    {
        return null;
    }
}



现在调用该函数并传递XML文件Path。例如:


Now call the function and pass XML file Path. Like:

protected void Page_Load(object sender, EventArgs e)
{
     DataTable dt = fnXMLToDataTable("MyFilePath")
     if(dt!=null)
     {
          DropDownList1.DataSource = dt;
          DropDownList1.DataTextField = "Name";
          DropDownList1.DataValueField = "Code";
          DropDownList1.DataBind();
     }
}







--Amit




--Amit