Reputation: 1546
simple form: reverse of this I am currently working on reading in a xml files I created into my program (that part is working fine), and then applying the values to datums within my program. the example xml is
<Data>
<majorItem1>15</majorItem1>
<majorItem2>22.6</majorItem2>
<majorItem3>this string</majorItem3>
<List>
<items>
<item1>2</item1>
<item2>x1</item2>
<item3>4</item3>
</items>
<items>
<item1>5</item1>
<item2>x2</item2>
<item3>28</item3>
</items>
<items>
<item1>12</item1>
<item2>x3</item2>
<item3>100</item3>
</items>
</List>
</Data>
that needs to be parsed, and evaluated specifically (I do need it to be that rigid) my current abstracted code is:
using System;
using System.Collections.Generic;
using System.Linq;
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;
int inItem1;
float majorItem2 = 22.6f;
float inItem2;
string majorItem3 = "this string";
string inItem3;
List<things> items = new List<things>();
List<things> reItems = 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",
from _t in items
select new XElement("items",
new XElement("item1", _t.item1),
new XElement("item2", _t.item2),
new XElement("item3", _t.item3)
)
)
)
);
// portion in question starts here. everthing above is working fine
Console.WriteLine(doc.ToString());
XDocument doc2 = new XDocument(doc);
// null reference exception is thrown here
inItem1 = Convert.ToInt32(doc2.Element("majorItem1").Value);
inItem2 = Convert.ToSingle(doc2.Element("majorItem2").Value);
inItem3 = doc2.Element("majorItem3").Value;
foreach(XElement xe in doc2.Descendants("items")) {
things _thing = new things();
_thing.item1 = Convert.ToInt32(xe.Element("item1").Value);
_thing.item2 = xe.Element("item2").Value;
_thing.item3 = Convert.ToSingle(xe.Element("item3").Value);
}
Console.WriteLine(doc2.ToString());
Console.WriteLine("inItem1: " + inItem1.ToString());
Console.WriteLine("inItem2: " + inItem2.ToString());
Console.WriteLine("inItem3: " + inItem3);
foreach(things _t in reItems) {
Console.WriteLine("item1: " + _t.item1.ToString());
Console.WriteLine("item2: " + _t.item2);
Console.WriteLine("item3: " + _t.item3.ToString());
}
}
}
}
In the final code I will be reading the xml from a real file that the program has created to populate fields within the program everything is in place except the import from xml file.
if I remove the .Value from the assignments it no longer give nullReferenceExceptions, but it then gives garbage because it is just translating the whole thing with the casts.
any help would be greatly appreciated.
Upvotes: 0
Views: 37
Reputation: 89295
XDocument.Element("element_name")
only works when <element_name>
is the root element of the XDocument
. In your case, the target elements are child of the root element. So, as exemplified in the answer to the linked question, you need to get your target element via Root
, for example :
inItem1 = (int)doc2.Root.Element("majorItem1");
Upvotes: 3