将子组添加到随机空节点

时间:2016-06-04 20:16:35

标签: c# xml linq-to-xml

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

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

    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>
    

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

1 个答案:

答案 0 :(得分:1)

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

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