将儿童组添加到随机的空节点 [英] adding groups of children to random empty node

查看:70
本文介绍了将儿童组添加到随机的空节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用XDocument为我的程序创建一个xml文档作为保存结构.创建文档并播种已知数据是可以的,但是当涉及到从一个或多个struct列表中向文档中创建数据时,我遇到了一系列问题

I am attempting to create an xml document as a save structure for my program using XDocument. creating the document, and seeding the known data is fine, but when it comes to creating data into the document from a list of struct(s) I run into a set of issues

  1. 尝试访问专门为插入而创建的节点时,出现"NullReferenceException".
  2. 没有关于将子代添加到随机空节点的明确文档.到目前为止,我发现的所有内容都是在将已知值添加到预先存在的容器中.找到某物可能在这里起作用,但我不知道如何适应答案(我从未学习过操作的"=>"结构)
  1. I am getting 'NullReferenceException' when trying to access my node created specifically for insertion.
  2. there is no clear documentation on adding children to a random empty node. everything I have found so far is on adding known values to preexisting containers. found something that might work here, but I don't understand how to adapt the answer (I never learned the "=>" structure of operations)

我当前的示例程序如下:

my current example program is as such:

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();

        }
    }
}

我希望能够将多个子节点插入标有列表"的空节点中

I want to be able to insert multiple child nodes into the empty node labeled "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>

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

how can I insert multiple child nodes to a known random node that will only be empty for the first insertion. Thank you for your help.

推荐答案

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

You can populate <List> element with items when the XML document is created for the first time like so :

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

dotnetfiddle demo 1

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

Or, if you need to add <item> to existing <List> element instead :

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

dotnetfiddle demo 2

这篇关于将儿童组添加到随机的空节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆