C# XML LINQ Query to LIST<>

banqimula

New Member
I'm trying to turn an XML document into a List<> with a predefined ClassMy xml dataset TestData.xml\[code\]<Objects> <Object ID="1"> <ItemOne>"Hickory"</ItemOne> <ItemTwo>"Dickory"</ItemTwo> <ItemThree>"Dock"</ItemThree> </Object> <Object ID="2"> <ItemOne>"The"</ItemOne> <ItemTwo>"Mouse"</ItemTwo> <ItemThree>"Went"</ItemThree> </Object></Objects>\[/code\]The Main program\[code\]class Program{ static void Main(string[] args) { XElement TestData = http://stackoverflow.com/questions/13740247/XElement.Load("TestData.xml"); List<Test> myTest = new List<Test>(from d in TestData.Descendants("Objects") select new Test( d.Element("ItemOne").Value.ToString(), d.Element("ItemTwo").Value.ToString(), d.Element("ItemThree").Value.ToString())); myTest.ForEach(i => Console.WriteLine("{0} {1} {2}", i.itemOne, i.itemTwo, i.itemThree)); Console.ReadLine(); }}\[/code\]The class I'm trying to form the data to\[code\]class Test{ public string itemOne { get; set; } public string itemTwo { get; set; } public string itemThree { get; set; }}\[/code\]I would like to get out\[code\]Hickory Dickory DockThe Mouse went\[/code\]but I end up getting nothing. It looks like the LINQ Query finds the data but never assigns it to the \[code\]List<Test>\[/code\] myTest, the value shows as null in the debugger. I'm not sure what I'm doing wrong.I want to convert the XML to \[code\]List<Test>\[/code\] so I can randomly rearrange through the objects. I was going to just use int[] array and sort by "ID" but I don't know the actual length of the array and want to assign it on the fly so I am forced to do a \[code\]list<>\[/code\]. I am open to other suggestions of accomplishing this.
 
Back
Top