【问题标题】:How to read xml attributes into 2d list List<List<Double>>如何将 xml 属性读入二维列表 List<List<Double>>
【发布时间】:2015-01-16 00:37:40
【问题描述】:

我正在制作一个应用程序,它从文本框中获取值并将它们保存到 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();
    }

【问题讨论】:

  • 你能发布输入的xml吗?
  • 输入是什么意思?我用来写入 xml 的代码?还是xml文件本身?
  • 您在 XDocument 中加载的 xml 文档。
  • @PankajKapare 添加了它!

标签: c# xml list


【解决方案1】:

请纠正以下内容。

  1. 如下初始化列表

    列表子列表=新列表(){0,0,0,0,0,0};

  2. “Electricty”拼写错误。修复 xml。

【讨论】:

  • 它不允许我把 {} 放在 () 之后?
  • 我不知道,但不知何故,stackoverflow 编辑器搞砸了这一行。改正了。
【解决方案2】:

这是您可能更喜欢的另一种方法。

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&lt;List&lt;double&gt;&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多