I have the sample xml
<ListingDoc version="1.0">
<Areas>
<Area Area_Seq="1" Area_Name="Mumbai" Area_Code="MUM"/>
<Area Area_Seq="1" Area_Name="Delhi" Area_Code="DEL"/>
</Areas>
<Companies>
<Company Company_Name="ABCD" Company_Rating="5" Company_Parent=""/>
<Company Company_Name="XYZ" Company_Rating="12" Company_Parent="ABCD"/>
<Company Company_Name="MAN" Company_Rating="77" Company_Parent=""/>
</Companies>
</ListingDoc>
and I want to use serialize this xml in corresponding objects using c#. but when I do this only alternate rows are coming in object. I used the code written below
XmlDataDocument xmldoc = new XmlDataDocument();
FileStream xmlFile=null;
xmlFile = new FileStream("c://temp//Listing.xml", FileMode.Open, FileAccess.Read);
using (xmlFile)
{
XmlNode n1= xmldoc.DocumentElement;
XmlNodeList nodes = n1.SelectNodes("Companies");
if (nodes != null && nodes.Count > 0)
{
//log session node found
XmlDataDocument companyXml= new XmlDataDocument();
companyXml.LoadXml(nodes[0].OuterXml);
XmlNode Tag_comp = companyXml.DocumentElement;
XmlReader xmlReader = new XmlNodeReader(Tag_comp);
List<Company> companyList=new List<Company>();
using (xmlReader)
{
while (xmlReader.Read())
{
if (xmlReader.AttributeCount > 0)
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(Company));
var session = (Company)ser.Deserialize(xmlReader);
companyList.Add(session);
}
}
}
}
}
This populates my list with alternative rows. Please suggest me something to fix it as I found that when I serialize my row then xmlreader advance to next record and I have used xmlReader.Read() in while loop also.
Alternatively I tried to use XDocument also. but it gave me error root element is missing so suggest me something .