gardian06
gardian06

Reputation: 1546

adding groups of children to random empty node

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. 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"

I can currently create:

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

what I want to change it to is this:

<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.

Upvotes: 0

Views: 73

Answers (1)

har07
har07

Reputation: 89305

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

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

Upvotes: 1

Related Questions