这是我想要取消的Xml文件:
<?xml version="1.0"?>
<AddressDirectory>
<Address>
<HouseNo>1</HouseNo>
<StreetName>Pitampura</StreetName>
<City>Delhi</City>
</Address>
<Address>
<HouseNo>4</HouseNo>
<StreetName>Rohini</StreetName>
<City>Delhi</City>
</Address>
</AddressDirectory>
这些是我的课程:
public class AddressDirectory
{
[XmlElement("Address")]
public List<Address> addressList = new List<Address>();
}
public class Address
{
public int HouseNo { get; set; }
public string StreetName { get; set; }
public string City { get; set; }
}
这是我的主要内容:
XmlSerializer deserializer = new XmlSerializer(typeof(AddressDirectory));
TextReader reader = new StreamReader(@"D:\myXml.xml");
object obj = deserializer.Deserialize(reader);
AddressDirectory XmlData = (AddressDirectory)obj;
reader.Close();
这就是我访问必须保存在列表中的元素的方式:
XmlData.addressList[0].HouseNo;
XmlData.addressList[0].StreetName;
XmlData.addressList[0].City;
或在循环中,我这样访问:
for (int i = 1; i < 10; i++)
{
Console.Write("house number for {0}: ", i);
Console.WriteLine(XmlData.addressList[i].HouseNo);
Console.Write("Street Name for {0}: ", i);
Console.WriteLine(XmlData.addressList[i].StreetName);
Console.Write("City for {0}: ", i);
Console.WriteLine(XmlData.addressList[i].City);
}
这是我执行程序时遇到的错误:
指数超出范围。必须是非负数且小于 集合。
我想这是因为我们没有在列表中添加任何内容。因为要添加到列表中,我们必须在main中的某处使用List.Add()。
这是编辑后的主要():
XmlSerializer deserializer = new XmlSerializer(typeof(AddressDirectory));
TextReader reader = new StreamReader(@"D:\addressDirectory.xml");
object obj = deserializer.Deserialize(reader);
AddressDirectory XmlData = (AddressDirectory)obj;
reader.Close();
for (int i = 0; i < XmlData.addressList.Count; i++)
{
Console.WriteLine("Information for item #{0}", i + 1);
Console.WriteLine(" - House Number ... {0}", XmlData.addressList[i].HouseNo);
Console.WriteLine(" - Street Name .... {0}", XmlData.addressList[i].StreetName);
Console.WriteLine(" - City ........... {0}", XmlData.addressList[i].City);
}
Console.ReadLine();
现在它不会出现任何错误,也不会显示列表的内容
答案 0 :(得分:1)
尝试将XmlData.addressList.Count
属性用于for条件:
for (int i = 0; i < XmlData.addressList.Count; i++)
{
Console.WriteLine("Information for item #{0}", i + 1);
Console.WriteLine(" - House Number ... {0}", XmlData.addressList[i].HouseNo);
Console.WriteLine(" - Street Name .... {0}", XmlData.addressList[i].StreetName);
Console.WriteLine(" - City ........... {0}\n", XmlData.addressList[i].City);
}
以下是另一种方法,以及我用来测试它的其余代码(适用于您的xml文件和类):
public static void Main()
{
var addresses = new List<Address>();
var deserializer = new XmlSerializer(typeof(AddressDirectory));
using (var reader = new StreamReader(@"C:\Users\Rufus\Documents\temp\temp.xml"))
{
addresses.AddRange(
((AddressDirectory)deserializer.Deserialize(reader)).addressList);
}
int counter = 1;
foreach (var address in addresses)
{
Console.WriteLine("Information for item #{0}", counter++);
Console.WriteLine(" - House Number ... {0}", address.HouseNo);
Console.WriteLine(" - Street Name .... {0}", address.StreetName);
Console.WriteLine(" - City ........... {0}\n", address.City);
}
}