本文介绍了从Xml数据内容中拆分一个值并添加下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?xml version="1.0" encoding="utf-8" ?>
<Title>
<subtitle>12,15,1,4 </subtitle>
</Title >
This is my xml structure and i need to split the value and add in dropdownlist, but iam getting whole value in dropdownlist, kindly provide me the solution and my code is
private void bindxml()
{
StreamReader str = new StreamReader(filepath);
using (DataSet ds = new DataSet())
{
ds.ReadXml(filepath);
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "subtitle";
DropDownList1.DataBind();
}
}
推荐答案
var codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
var pathname = Path.GetDirectoryName(path) + @"\test.xml";
var doc = new XmlDocument();
doc.Load(pathname);
XmlNode node = doc.DocumentElement.SelectSingleNode("/Title/subtitle");
var csv = node.InnerText;
DataTable table = new DataTable();
string[] valarr = csv.Split(',');
table.Columns.Add("subtitle", typeof(string));
for (int i = 0; i < valarr.Length; i++)
{
table.Rows.Add(valarr[i]);
}
var a = table;
Console.ReadKey();
//DropDownList1.DataSource = ds;
//DropDownList1.DataTextField = "subtitle";
//DropDownList1.DataBind();
谢谢
Thanks
这篇关于从Xml数据内容中拆分一个值并添加下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!