从Xml加载和处理键/值对 [英] Load and Process Key/Value Pair from Xml
问题描述
我有以下以键/值对形式的xml文件:
I have the following xml file in key/value pair form:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<root>
<row>
<var name="CountryId" value="1" />
<var name="Country" value="Afghanistan" />
<var name="FIPS104" value="AF" />
</row>
<row>
<var name="CountryId" value="2" />
<var name="Country" value="Albania" />
<var name="FIPS104" value="AL" />
</row>
<row>
<var name="CountryId" value="3" />
<var name="Country" value="Algeria" />
<var name="FIPS104" value="AG" />
</row>
</root>
我需要加载此xml文档,进行处理,然后通过Microsoft Visual Web Developer 2010将其填充到网页上的下拉列表中.
我试图通过XmlNodeList并遍历每个XmlNode来做到这一点,但是我尝试过的一切都给了我错误!
这是我的.cs文件中的代码.
I need to load this xml document, process it and populate it in a dropdownlist on a webpage via Microsoft Visual Web Developer 2010.
I was trying to do it via XmlNodeList and looping through each XmlNode, but everything I''ve tried has given me errors!
This is the code in my .cs file.
// Country processing
protected void SetCountryDropDownList()
{
XmlDocument xmlCountryDocument = new XmlDocument();
xmlCountryDocument.Load(XML_FILE_PATH2);
if (!(xmlCountryDocument == null))
{
XmlNodeList countryList = xmlCountryDocument.SelectNodes("root/row");
foreach (XmlNode countryNode in countryList)
{
if (countryNode.Name == "CountryId")
DropDownListCountry.Items.Add(countryNode.Value.ToString());
}
}
}
这是我的.aspx文件中的代码.
This is the code in my .aspx file.
<tr>
<td align="left" class="paddingRight"></td>
<td align="left" class="paddingRight">
<asp:label ID="LabelCountry" class="lbl" text="Country:" runat="server" xmlns:asp="#unknown"></asp:label>
</td>
<td>
<asp:DropDownList ID="DropDownListCountry" class="droplist" xmlns:asp="#unknown" Width="195px"
ToolTip="Select the country where the trailer currently resides" runat="server"
DataTextField="Country" DataValueField="CountryId" AutoPostBack="True"
AppendDataBoundItems="True" onselectedindexchanged="DropDownListCountry_SelectedIndexChanged"></asp:DropDownList>
</td>
<td align="left" class="paddingRight"></td>
</tr>
我知道我做错了,但我无法弄清楚!
我尝试使用IDictionary ...,即
IDictionary<string, string> keyValuePairList = new Dictionary<string, string>();
并尝试对其进行处理,但是我又出现了错误.
任何帮助将不胜感激!
谢谢!
Cindy
I know that I''m doing something wrong, but I can''t figure it out!
I''ve tried using IDictionary...i.e.,
IDictionary<string, string> keyValuePairList = new Dictionary<string, string>();
and trying to process it, but I''m coming back with errors.
Any help would be greatly appreciated!!
Thanks!
Cindy
推荐答案
Cindy,
您可以尝试这段代码吗?
网页:
Hi Cindy,
Could you please try this piece of code.
Web page :
<asp:DropDownList runat="server" ID="DropDownListCountry" DataTextField="CountryName" DataValueField="CountryId"></asp:DropDownList>
背后的代码:
Code behind :
protected void Page_Load(object sender, EventArgs e)
{
LoadCountry();
}
void LoadCountry()
{
var countries = GetCountries();
DropDownListCountry.DataSource = countries;
DropDownListCountry.DataBind();
}
private IList<Country> GetCountries()
{
string XML_FILE_PATH2 = "C:\\Temp\\Websites\\TrailerInitializeTest\\Resources\\Countries.xml";
//string XML_FILE_PATH2 = Path.Combine(Server.MapPath("~/Resources"), "Countries.xml");
IList<Country> countries = new List<Country>();
XmlDocument xmlCountryDocument = new XmlDocument();
xmlCountryDocument.Load(XML_FILE_PATH2);
if (!(xmlCountryDocument == null))
{
XmlNodeList countryList = xmlCountryDocument.SelectNodes("root/row");
foreach (XmlNode countryNode in countryList)
{
string countryId = string.Empty;
string country = string.Empty;
string countryCode = string.Empty;
foreach (XmlNode varElement in countryNode.ChildNodes)
{
switch (varElement.Attributes["name"].Value)
{
case "CountryId":
countryId = varElement.Attributes["value"].Value;
break;
case "Country":
country = varElement.Attributes["value"].Value;
break;
case "FIPS104":
countryCode = varElement.Attributes["value"].Value;
break;
}
}
countries.Add(new Country()
{
CountryId = countryId,
CountryName = country,
CountryCode = countryCode
});
}
}
return countries;
}
public class Country
{
public string CountryName { get; set; }
public string CountryId { get; set; }
public string CountryCode { get; set; }
}
如果可以解决您的问题,请标记您的问题为解决.
Please mark your question as solve if this solve your issue.
这篇关于从Xml加载和处理键/值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!