检查这个
你也可以检查你写“阅读器”时得到的选项。
xml文件
<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>
<ISO_3166-1_List_en xml:lang="en">
<ISO_3166-1_Entry>
<ISO_3166-1_Country_name>SINT MAARTEN</ISO_3166-1_Country_name>
<ISO_3166-1_Alpha-2_Code_element>SX</ISO_3166-1_Alpha-2_Code_element>
</ISO_3166-1_Entry>
<ISO_3166-1_Entry>
<ISO_3166-1_Country_name>SLOVAKIA</ISO_3166-1_Country_name>
<ISO_3166-1_Alpha-2_Code_element>SK</ISO_3166-1_Alpha-2_Code_element>
</ISO_3166-1_Entry>
</ISO_3166-1_List_en>
和读者真的很基本但很快
XmlTextReader reader = new XmlTextReader("c:/countryCodes.xml");
List<Country> countriesList = new List<Country>();
Country country=new Country();
bool first = false;
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
if (reader.Name == "ISO_3166-1_Entry") country = new Country();
break;
case XmlNodeType.Text: //Display the text in each element.
if (first == false)
{
first = true;
country.Name = reader.Value;
}
else
{
country.Code = reader.Value;
countriesList.Add(country);
first = false;
}
break;
}
}
return countriesList;