先生,下面是我的xml文件,现在我需要在下拉列表中获取ID值 [英] Sir below is my xml file now i need to fetch ID values in Dropdown List
本文介绍了先生,下面是我的xml文件,现在我需要在下拉列表中获取ID值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<?xml version="1.0" encoding="utf-8" ?>
<consumers>
<consumer ID="1" Name="Pravin">
<MeterNumber>9611186466</MeterNumber>
<Address>Jayanagar 7th block</Address>
<DCUName>DCUCMTRXXX00011</DCUName>
</consumer>
<consumer ID="2" Name="Praveen">
<MeterNumber>9611186466</MeterNumber>
<Address>Jayanagar 5th block</Address>
<DCUName>SPPLCMTRXXX00031</DCUName>
</consumer>
</consumers>
推荐答案
这是一个非常简单的任务,这是可能的方法之一:
It is a super simple task, this is one of possible approaches:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="XmlDataSource1" DataTextField="Name" DataValueField="ID">
</asp:DropDownList>
<asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/test.xml"
XPath="consumers/consumer"></asp:XmlDataSource>
将此代码复制到页面中,并确保您的文件名为test.xml,并且位于站点根目录中.
欢呼
Copy this code in a page and make sure that your file is called test.xml and that it is in the site root directory.
Cheers
这里有另一种方式,
XmlDocument doc =新的XmlDocument();
字符串路径= Server.MapPath(〜/test.xml");//您需要获取值的xml文件的路径.
doc.Load(path);
DataSet _ds = new DataSet();
_ds.ReadXml(path);
yourdropdown.DataSource = _ds.Tables [0];
yourdropdown.datatextfield ="ID";
yourdropdown.datavaluefield ="Name";
yourdropdown.DataBind();
Heres another way,
XmlDocument doc = new XmlDocument();
string path = Server.MapPath("~/test.xml");//path of xml file that you need to fetch values.
doc.Load(path);
DataSet _ds = new DataSet();
_ds.ReadXml(path);
yourdropdown.DataSource = _ds.Tables[0];
yourdropdown.datatextfield="ID";
yourdropdown.datavaluefield="Name";
yourdropdown.DataBind();
这篇关于先生,下面是我的xml文件,现在我需要在下拉列表中获取ID值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文