将子组添加到随机空节点

adding groups of children to random empty node

我正在尝试使用 XDocument 创建一个 xml 文档作为我的程序的保存结构。创建文档并播种已知数据很好,但是当涉及到从结构列表中将数据创建到文档中时,我 运行 遇到了一系列问题

  1. 我在尝试访问我专门为插入创建的节点时收到 'NullReferenceException'。
  2. 没有关于将子节点添加到随机空节点的明确文档。到目前为止,我发现的所有内容都是将已知值添加到预先存在的容器中。找到 something that might work here,但我不明白如何调整答案(我从未学过“=>”运算结构)

我当前的示例程序是这样的:

using System;
using System.Collections.Generic;
using System.Xml.Linq;

namespace xmlTest {
    public struct things {
        public int item1;
        public string item2;
        public float item3;
    }

    class Program {

        static void Main(string[] args) {
            int majorItem1 = 15;
            float majorItem2 = 22.6f;
            string majorItem3 = "this string";
            List<things> items = new List<things>();

            things x1 = new things();
            x1.item1 = 2;
            x1.item2 = "x1";
            x1.item3 = 4;
            items.Add(x1);

            things x2 = new things();
            x2.item1 = 5;
            x2.item2 = "x2";
            x2.item3 = 28;
            items.Add(x2);

            things x3 = new things();
            x3.item1 = 12;
            x3.item2 = "x3";
            x3.item3 = 100;
            items.Add(x3);

            XDocument doc = new XDocument(
                new XDeclaration("1.0", "utf=8", "true"),
                new XElement("Data",
                    new XElement("majorItem1", majorItem1),
                    new XElement("majorItem2", majorItem2),
                    new XElement("majorItem3", majorItem3),
                    new XElement("List", "")
                    )
                );

            Console.WriteLine(doc.ToString());
            Console.ReadLine();

            int ii = 0;                                     //intended for tracking foreach position
            foreach(things _t in items)                     //have issues with inserting many, so try inserting 1
            //things _t = items[0];                       //test for single item
            {
                doc.Element("List").Add(new XElement("item",//from what I can tell it is having trouble finding
                    new XElement("item1", _t.item1),        //"list" so it is thowing a NullReference
                    new XElement("item2", _t.item2),
                    new XElement("item3", _t.item3)
                    )
                );
                ii++;                                       //foreach tracking

            }
            Console.WriteLine(doc.ToString());
            Console.ReadLine();

        }
    }
}

我希望能够将多个子节点插入到标记为 "List"

的空节点中

我目前可以创建:

<Data>
    <majorItem1>15</majorItem1>
    <majorItem2>22.6</majorItem2>
    <majorItem3>this string</majorItem3>
    <List></List>
</Data>

我想改成这样:

<Data>
    <majorItem1>15</majorItem1>
    <majorItem2>22.6</majorItem2>
    <majorItem3>this string</majorItem3>
    <List>
        <item>
            <item1>2</item1>
            <item2>x1</item2>
            <item3>4</item3>
        </item>
        <item>
            <item1>5</item1>
            <item2>x2</item2>
            <item3>28</item3>
        </item>
        <item>
            <item1>12</item1>
            <item2>x3</item2>
            <item3>100</item3>
        </item>
    </List>
</Data>

如何将多个子节点插入到一个已知的随机节点,该节点在第一次插入时仅为空。感谢您的帮助。

当第一次创建 XML 文档时,您可以使用 items 填充 <List> 元素,如下所示:

XDocument doc = new XDocument(
    new XDeclaration("1.0", "utf=8", "true"),
    new XElement("Data",
                 new XElement("majorItem1", majorItem1),
                 new XElement("majorItem2", majorItem2),
                 new XElement("majorItem3", majorItem3),
                 new XElement("List", 
                    from item in items
                    select new XElement("item",
                        new XElement("item1", item.item1),
                        new XElement("item2", item.item2),
                        new XElement("item3", item.item3)
                    )
                 )
    )
);

dotnetfiddle demo 1

或者,如果您需要将 <item> 添加到现有的 <List> 元素中:

doc.Root
   .Element("List")
   .Add(from item in items
        select new XElement("item",
                             new XElement("item1", item.item1),
                             new XElement("item2", item.item2),
                             new XElement("item3", item.item3)
                            ));

dotnetfiddle demo 2