先生,下面是我的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();