本文介绍了将数据绑定到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 .
推荐答案
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
这篇关于将数据绑定到xml的下拉列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!