I am reading a huge XML file (250 MB) using XMLReader and add only specific elements to a generic List . I am unable to add values to the list correctly.I am getting null values in the List.I'll appreciate your help Below is the class I am using :
public class SomeInfo
{
public string Item1 { get; set; }
public string Item2 { get; set; }
}
My code to read XML is as below :
using (XmlReader reader = XmlReader.Create(file)
)
{
List<SomeInfo> test = new List<SomeInfo>();
while (reader.Read())
{
var testObject = new SomeInfo();
if (reader.NodeType == XmlNodeType.Element)
{
string name = reader.Name;
switch (name)
{
case "Item1":
reader.Read();
testObject.item1= reader.Value;
break;
case "Item2":
reader.Read();
testObject.item2= reader.Value;
break;
}
test.Add(testObject);
}
}
Sample XML : This is huge xml file and I only need to read some elements and add to the list .In my code above, I am only reading Item1 and Item2 and do not care about Xitem,Bitem and Citem tags
<mainItem>
<Item>
<Xitem>125</Xitem>
<Item1>ab41gh80020gh140f6</Item1>
<BItem>42ae3de3</BItem>
<Item2>7549tt80384</Item2>
<Citem>c7dggf66e4</Citem>
</Item>
<Item>
<Xitem>865</Xitem>
<Item1>ab41gh80020gh140f6</Item1>
<BItem>42aejj3de3</BItem>
<Item2>7549kljj80384</Item2>
<Citem>c7df6kk6e4</Citem>
</Item>
<Item>
<Xitem>895</Xitem>
<Item1>ab41gjgjgh80020gh140f6</Item1>
<BItem>42aehkh3de3</BItem>
<Item2>754980384</Item2>
<Citem>c7dfjj66e4</Citem>
</Item>
</mainItem>