Click here to Skip to main content
15,879,535 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Following is my xml file called PropertyInfo.xml :-
XML
<?xml version="1.0" encoding="utf-8" ?>
<PropertyInformation>
<locations>
  <location name="Bombay">
    <Buildings>
      <Building name="Majestic House">
        <Rooms>
          <Room name="Delmonte Conference Room">
            <Capacity>325</Capacity>
          </Room>
          <Room name="Majestic Ballroom">
            <Capacity>150</Capacity>
          </Room>
        </Rooms>
      </Building>
      <Building name="Taj Mahal Palace Hotel">
        <Rooms>
          <Room name="THE LUXURY GRANDE">
            <Capacity>4</Capacity>
          </Room>
        </Rooms>
      </Building>
    </Buildings>
  </location>
  <location name="New York">
    <Buildings>
      <Building name="City Group Center">
        <Rooms>
          <Room name="LeMessurier Hall">
            <Capacity>210</Capacity>
          </Room>
        </Rooms>
      </Building>
    </Buildings>
  </location>
</locations>
</PropertyInformation>

I created following 4 Models to map above xml file :-
C#
public class Room
{
  [XmlAttribute("name")]
  public String Name { get; set; }
  public int Capacity { get; set; }
}

public class Building
{
  [XmlAttribute("name")]
  public String Name { get; set; }
  public List<Room> Rooms { get; set; }
}

public class Location
{
  [XmlAttribute("name")]
  public string Name { get; set; }
  public Building Buildings { get; set; }
}

[XmlRoot("PropertyInformation")]
public class PropertyInformation
{
  [XmlArray("locations")]
  [XmlArrayItem("location")]
  public List<Location> Locations { get; set; }
}

I created following DAL class to load the information from PropertyInfo.xml :-
C#
public class PropertyInfoDal
{
  public List<Location> LoadPropertyInfo()
  {
    DataSet ds = new DataSet();
    XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataSet));
    FileStream readStream = new FileStream("~/App_Data/PropertyInfo.xml", FileMode.Open);
    ds = (DataSet) xmlSerializer.Deserialize(readStream);
    readStream.Close();

    //How to Cast DataSet to Strongly Typed Object( List<Location>) before it gets returned ??
    //I started with this much below
    List<Location> locations = (from row in ds.Tables[0].AsEnumerable()
    select new PropertyLocation
    {
      Name = row.Field<String>("Name"),
      Buildings = ?????
    }).ToList();

    return locations;
  }
}

In above LoadPropertyInfo() method, how to cast DataSet to List<Location> locations before returning "locations" ? Pls help. Thanks!
Posted
Updated 17-Dec-14 10:05am
v5
Comments
Richard MacCutchan 17-Dec-14 15:33pm    
You have tagged this VB.NET and C#; don't you know which?

Why to bother with the dataset at all. You can deserialize directly to your objects, like this:
C#
var xmlSerializer = new XmlSerializer(typeof(PropertyInformation));
var propInfo = (PropertyInformation) xmlSerializer.Deserialize(readStream);
return propInfo.Locations; 

You need to add couple of attributes to your classes however:
C#
public class Room
{
  [XmlAttribute("name")]
  public String Name { get; set; }
  public int Capacity { get; set; }
}
 
public class Building
{
  [XmlAttribute("name")]
  public String Name { get; set; }
  [XmlArray("Rooms")]
  [XmlArrayItem("Room")]
  public List<Room> Rooms { get; set; }
}
 
public class Location
{
  [XmlAttribute("name")]
  public string Name { get; set; }
  [XmlArray("Buildings")]
  [XmlArrayItem("Building")]
  public List<Building> Buildings { get; set; }
}
 
[XmlRoot("PropertyInformation")]
public class PropertyInformation
{
  [XmlArray("locations")]
  [XmlArrayItem("location")]
  public List<Location> Locations { get; set; }
}
 
Share this answer
 
v2
Comments
samkernel 17-Dec-14 17:08pm    
Thanks, Tomas!
Maciej Los 17-Dec-14 17:35pm    
My 5, but...
Why do you gave Him/Her a fish instead a fishing rod?
Tomas Takac 17-Dec-14 18:30pm    
Thanks Maciej. I see your point. I generally strive to provide a complete solution. In some cases however it is counterproductive I agree. Looking back this one is one of them as it could be solved using google. I guess answering questions is skill like any other and I need to figure out the balance between fish and rod yet.
Maciej Los 18-Dec-14 4:04am    
;)
Your definition of entity model is wrong. I'd suggest to use xsd.exe[^], which helps to create class(es) related to xml schema.

For further information, please see:
Auto generating Entity classes with xsd.exe for XML Serialization and De-Serialization[^]
XML Serialization and Deserialization: Part-1[^]
XML Serialization and Deserialization: Part-2[^]

Here is A Complete Sample of Custom Class Collection Serialization and Deserialization[^], although in VB.NET, but this language is similar to C#.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900