繁体   English   中英

c#在保存到文件之前附加XDocument

[英]c# Append XDocument Before Saving to File

我是一名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");
}

对于ListView的每个新行,将需要增加ListItem_1(例如,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。

谢谢你的帮助!

因此,在进行了更多的研究和研究之后,我找到了自己问题的答案。

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");
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM