【问题标题】:c# Append XDocument Before Saving to Filec# 在保存到文件之前附加 XDocument
【发布时间】:2018-08-07 14:43:08
【问题描述】:

我是 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# xml linq


    【解决方案1】:

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

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

    【讨论】:

    • 此类 XML:ListItem_{N} - 难以解析和处理。最好添加IdIndex 属性。
    • 难解析是什么意思?因为它是字符串而不是整数?
    猜你喜欢
    • 1970-01-01
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多