jeudi 9 juillet 2015

Parsing xml data and showing in the view

I have the xml file in the local drive and want to show in the view

 <?xml version="1.0" encoding="utf-8"?> <Houses>  <House name="House Country1">  <member>  <Name>A</Name>  </member>  <member>  <Name>B</Name>  </member>  </House>  <House name="House Country2">  <member>  <Name>C</Name>  </member>  <member>  <Name>D</Name>  </member>  </House>  </Houses>

My target is to parse the data in the xml file and put in a model

The model is

public class HouseModel
{
  public string HouseName {get;set;}
  public List<string> MemberNames {get;set;}

}

I am trying the following code:

XDocument feedXml = XDocument.Load(@"C:\HouseInfo.xml");
            var feeds = from feed in feedXml.Descendants("House")

                    select new 

                    {

                        Name = feed.Element("Member").Value



                    };

This is give the output as A and C.

Is there any way to parse this xml file and extract the data into the model HouseModel like HouseName "House Country1" should contain members A and B , similarly with "House Country2" as well?

Aucun commentaire:

Enregistrer un commentaire