0

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 .

1 Answer 1

1

Seems to me you are doing to much yourself. Let the framework handle the XML to Object conversion for you all the way.

The following example uses a custom path (so change it), but it does assume your XML structure. This should give enough hints to continue on your way.

class Program
{
    const string filename = @".\Example.xml";
    static void Main(string[] args)
    {
        XmlSerializer xSer = new XmlSerializer(typeof(ListingDoc), new Type[] { typeof(Company), typeof(Area) });
        using (FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read))
        {
            // load from disk into object model
            ListingDoc listing = xSer.Deserialize(fs) as ListingDoc;

            // output loaded info
            listing.Areas.ForEach(area => Console.WriteLine("Area: {0}, {1}, {2}", area.Name, area.Sequence, area.Code));

            listing.Companies.ForEach(company => Console.WriteLine("Companies: {0}, {1}, {2}", company.Name, company.Rating, company.Parent));

        }
    }
}

public class ListingDoc
{
    public List<Area> Areas;
    public List<Company> Companies;
}

public class Company
{
    [XmlAttribute("Company_Rating")]
    public int Rating;

    [XmlAttribute("Company_Name")]
    public string Name;

    [XmlAttribute("Company_Parent")]
    public string Parent;
}

public class Area {
    [XmlAttribute("Area_Seq")]
    public int Sequence;

    [XmlAttribute("Area_Name")]
    public string Name;

    [XmlAttribute("Area_Code")]
    public string Code;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks alot. but i am getting exception. exception message is "There is an error in XML document (1, 2)" and inner exception is this. {"<ListingDoc xmlns=''> was not expected."}
Hi Marvin, Thanks alot. i resolved this issue.source :- stackoverflow.com/questions/4884383/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.