我想使用下面的xml和類反序列化一些xml。我遇到的問題是xml元素名稱與我想用於我的類的名稱不匹配。我試圖使用XmlRoot來指定一個元素名稱,但一直沒有能夠得到它的工作。任何幫助,將不勝感激。反序列化XML
<Results recordReturn="3" xmlns="http://www.zzz.com/"> <Result> <key>98937747-0596-42e6-aa5b-e180d35f649e</key> <code>AFGHANISTAN</code> <number>004</number> </Result> <Result> <key>100ab860-f2a5-48ed-911c-31753b79234f</key> <code>ALBANIA</code> <number>008</number> </Result> <Result> <key>67ecc235-d44a-41e0-b2a0-7a9c00e30a0e</key> <code>ALGERIA</code> <number>012</number> </Result>
[Serializable]
[XmlRoot("Result", Namespace = "http://www.zzz.com/")]
public class Country
{
public Country()
{ }
public string key;
public string code;
public string number;
}
[Serializable]
[XmlRoot(ElementName = "Results", Namespace = "http://www.zzz.com/")]
public class Countries : System.Collections.CollectionBase
{
public Country this[int nIndex]
{
get { return (Country)this.InnerList[nIndex]; }
}
public void Add(Country oCountry)
{
this.List.Add(oCountry);
}
}
//Code below is in separate class file
public static Countries GetAllCountries()
{
XmlNode countriesNode = //omitting code to get country xml
Countries countryList = new Countries();
XmlSerializer serializer = new XmlSerializer(typeof(Countries));
System.Xml.XmlNodeReader oReader = new System.Xml.XmlNodeReader(countriesNode);
countryList = (Countries)serializer.Deserialize(oReader);
return countryList;
}