首页 > 解决方案 > c# 在保存到文件之前附加 XDocument

问题描述

我是 C# 初学者,正在尝试使用 LINQ 来保存包含应用程序配置数据的 XML 文件。

我希望文件完成后结构看起来像这样:

<UABA>
  <Configure_Tab>
    <Actions_List>
      <ListItem_1 Position_X="1080" Position_Y="1920" RGB="255,255,255" Is_Colour="TRUE" Target="F1" Button="1" />
      <ListItem_2 Position_X="1080" Position_Y="1920" RGB="255,255,255" Is_Colour="TRUE" Target="F1" Button="1" />
      <ListItem_3 Position_X="1080" Position_Y="1920" RGB="255,255,255" Is_Colour="TRUE" Target="F1" Button="1" />
      <ListItem_4 Position_X="1080" Position_Y="1920" RGB="255,255,255" Is_Colour="TRUE" Target="F1" Button="1" />
    </Actions_List>
  </Configure_Tab>
</UABA>

我正在从 ListView 读取数据,现在我的代码如下所示:

public static void SaveConfiguration(ListViewItemCollection items)
{
    XDocument doc = new XDocument(new XElement("UABA",
                                       new XElement("Configure_Tab",
                                           new XElement("Actions_List",
                                           new XElement("ListItem_1", 
                                           new XAttribute("Position_X", "1080"),
                                           new XAttribute("Position_Y","1920"),
                                           new XAttribute("RGB", "255,255,255"),
                                           new XAttribute("Is_Colour", "TRUE"),
                                           new XAttribute("Target", "F1"),
                                           new XAttribute("Button", "1"))))));
    doc.Save("MySettings.xml");
}

ListItem_1 将需要为 ListView 的每个新行增加(例如,ListItem_2、ListItem_3 等),并且需要从当前行的 SubItems 中读取上述属性。我知道我可以使用这样的 for 循环:

for (int i = 0; i < items.Count; i++)
{  
    for (int j = 1; j < items[i].SubItems.Count; j++)
    {

    }
 }

其中“i”是当前 ListView 行,“j”是 SubItem 计数,但我不确定如何在写入文件之前附加当前 XDocument。

谢谢你的帮助!

标签: c#xmllinq

解决方案


因此,经过更多的研究和玩弄之后,我找到了自己问题的答案。

using System.Xml.Linq;
using System.Diagnostics;
using static System.Windows.Forms.ListView;

public static void SaveConfiguration(ListViewItemCollection items)
{
    XDocument doc = new XDocument(new XElement("UABA",
    new XElement("Configure_Tab",
    new XElement("Actions_List"))));

    for (int i = 0; i < items.Count; i++)
    {
        doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Add(new XElement("ListItem_" + i, items[i].Checked));

        for (int j = 0; j < items[i].SubItems.Count; j++)
        {
            if (j == 0)
                doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Element("ListItem_" + i).Add(new XAttribute("Position_X", items[i].SubItems[j].Text));
            if (j == 1)
                doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Element("ListItem_" + i).Add(new XAttribute("Position_Y", items[i].SubItems[j].Text));
            if (j == 2)
                doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Element("ListItem_" + i).Add(new XAttribute("RGB", items[i].SubItems[j].Text));
            if (j == 3)
                doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Element("ListItem_" + i).Add(new XAttribute("Is_Colour", items[i].SubItems[j].Text));
            if (j == 4)
                doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Element("ListItem_" + i).Add(new XAttribute("Target", items[i].SubItems[j].Text));
            if (j == 5)
                doc.Element("UABA").Element("Configure_Tab").Element("Actions_List").Element("ListItem_" + i).Add(new XAttribute("Press_Button", items[i].SubItems[j].Text));
        }
    }

    doc.Save("MySettings.xml");
}

推荐阅读