我正在创建一个应用程序,它从文本框中获取值并将它们保存到2D列表中。我得到它将列表保存到xml文件,但我无法将其添加回列表中。该属性存在。我使用了一个消息框弹出窗口来查看它是否正确地检索了属性。但出于某种原因,它没有将其添加到列表中。事实上,它甚至没有将其他信息加载到列表视图中。
public void Load(RadListView listView, List<List<double>> list)
{
XDocument doc = XDocument.Load(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "RentData.xml"));
foreach (var dm in doc.Descendants("Month"))
{
ListViewDataItem item = new ListViewDataItem();
foreach (XElement x in dm.Descendants())
{
List<double> sublist = new List<double>();
sublist.Capacity = 6;
if (x.Name == "Mike")
{
sublist.Insert(0, Convert.ToDouble(x.Attribute("Rent").Value));
sublist.Insert(2, Convert.ToDouble(x.Attribute("Internet").Value));
sublist.Insert(4, Convert.ToDouble(x.Attribute("Electricty").Value));
sublist.Insert(6, Convert.ToDouble(x.Attribute("Water").Value));
}
else if (x.Name == "Patti")
{
sublist.Insert(1, Convert.ToDouble(x.Attribute("Rent").Value));
sublist.Insert(3, Convert.ToDouble(x.Attribute("Internet").Value));
sublist.Insert(5, Convert.ToDouble(x.Attribute("Electricty").Value));
sublist.Insert(7, Convert.ToDouble(x.Attribute("Water").Value));
}
else
{
item.SubItems.Add(x.Value);
}
list.add(sublist);
}
item.TextAlignment = ContentAlignment.MiddleCenter;
listView.Items.Add(item);
}
}
编辑:Xml文件
<?xml version="1.0" encoding="utf-8"?>
<!--Rent Calculator Save Data-->
<Rent>
<Month>
<Date>01/15/2015</Date>
<Rent>$100.00</Rent>
<Internet>$110.00</Internet>
<Water>$120.00</Water>
<Electricity>$130.00</Electricity>
<totalExepense>$460.00</totalExepense>
<Payments>
<Patti Water="60" Electricity="65" Internet="55" Rent="50" />
<Mike Water="60" Electricity="65" Internet="55" Rent="50" />
</Payments>
</Month>
</Rent>
用于写入xml的代码:
public void Write(RadListView listView, List<List<double>> list)
{
XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };
XmlWriter writer = XmlWriter.Create(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "RentData.xml"), settings);
writer.WriteStartDocument();
writer.WriteComment("Rent Calculator Save Data");
writer.WriteStartElement("Rent");
for (int a = 0; a < listView.Items.Count; a++ )
{
foreach (ListViewDataItem i in listView.Items)
{
writer.WriteStartElement("Month");
writer.WriteElementString("Date", i[0].ToString());
writer.WriteElementString("Rent", i[1].ToString());
writer.WriteElementString("Internet", i[2].ToString());
writer.WriteElementString("Water", i[3].ToString());
writer.WriteElementString("Electricity", i[4].ToString());
writer.WriteElementString("totalExepense", i[5].ToString());
writer.WriteStartElement("Payments");
writer.WriteStartElement("Patti");
writer.WriteAttributeString("Water", list[a][6].ToString());
writer.WriteAttributeString("Electricity", list[a][4].ToString());
writer.WriteAttributeString("Internet", list[a][2].ToString());
writer.WriteAttributeString("Rent", list[a][0].ToString());
writer.WriteEndElement();
writer.WriteStartElement("Mike");
writer.WriteAttributeString("Water", list[a][7].ToString());
writer.WriteAttributeString("Electricity", list[a][5].ToString());
writer.WriteAttributeString("Internet", list[a][3].ToString());
writer.WriteAttributeString("Rent", list[a][1].ToString());
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
}
}
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
答案 0 :(得分:0)
请纠正以下事项。
按如下方式初始化列表
列表与LT;双&gt; sublist = new List&lt; double&gt;(){0,0,0,0,0,0};
&#34;有掉电&#34;拼写错误。修复xml。
答案 1 :(得分:0)
这是您可能更喜欢的替代方法。
var listOfListOfDouble =
doc
.Descendants("Month")
.Select(m =>
{
var d = m
.Elements("Payments")
.Elements()
.ToDictionary(
p => p.Name.LocalName,
p => p.Attributes()
.ToDictionary(
x => x.Name.LocalName,
x => (double)x));
return new []
{
d["Mike"]["Rent"],
d["Patti"]["Rent"],
d["Mike"]["Internet"],
d["Patti"]["Internet"],
d["Mike"]["Electricity"],
d["Patti"]["Electricity"],
d["Mike"]["Water"],
d["Patti"]["Water"],
}.ToList();
})
.ToList();
它适用于您提供的数据并返回List<List<double>>
。