473,386 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

How to cast Dataset to Strongly Typed Object ?

Following is my xml file called PropertyInfo.xml :-

Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <PropertyInformation>
  3.   <locations>
  4.     <location name="Bombay">
  5.       <Buildings>
  6.         <Building name="Majestic House">
  7.           <Rooms>
  8.             <Room name="Delmonte Conference Room">
  9.               <Capacity>325</Capacity>
  10.             </Room>
  11.             <Room name="Majestic Ballroom">
  12.               <Capacity>150</Capacity>
  13.             </Room>
  14.           </Rooms>
  15.         </Building>
  16.  
  17.         <Building name="Taj Mahal Palace Hotel">
  18.           <Rooms>
  19.             <Room name="THE LUXURY GRANDE">
  20.               <Capacity>4</Capacity>
  21.             </Room>
  22.           </Rooms>
  23.         </Building>
  24.       </Buildings>
  25.     </location>
  26.     <location name="New York">
  27.       <Buildings>
  28.         <Building name="City Group Center">
  29.           <Rooms>
  30.             <Room name="LeMessurier Hall">
  31.               <Capacity>210</Capacity>
  32.             </Room>
  33.           </Rooms>
  34.         </Building>
  35.       </Buildings>
  36.     </location>
  37.   </locations>
  38. </PropertyInformation>
I created following 4 Models to map above xml file :-
Expand|Select|Wrap|Line Numbers
  1. public class Room
  2. {
  3.         [XmlAttribute("name")]
  4.         public String Name { get; set; }
  5.         public int Capacity { get; set; }
  6. }
  7.  
  8.  public class Building
  9.  {
  10.         [XmlAttribute("name")]
  11.         public String Name { get; set; }
  12.         public List<Room> Rooms { get; set; }
  13.  }
  14.  
  15.  public class Location
  16.  {
  17.         [XmlAttribute("name")]
  18.         public string Name { get; set; }
  19.         public Building Buildings { get; set; }
  20.  }
  21.  
  22. [XmlRoot("PropertyInformation")]
  23.  public class PropertyInformation
  24.  {
  25.         [XmlArray("locations")]
  26.         [XmlArrayItem("location")]
  27.         public List<Location> Locations { get; set; }
  28.  }
  29.  
  30. I created following DAL class to load the information from PropertyInfo.xml :-
  31.  
  32. public class PropertyInfoDal
  33. {
  34.   public List<Location> LoadPropertyInfo()
  35.   {           
  36.     DataSet ds = new DataSet();
  37.     XmlSerializer  xmlSerializer = new XmlSerializer(typeof(DataSet));
  38.     FileStream readStream = new FileStream("~/App_Data/PropertyInfo.xml", FileMode.Open);
  39.     ds = (DataSet) xmlSerializer.Deserialize(readStream);
  40.     readStream.Close();
  41.  
  42.     //How to Cast DataSet to Strongly Typed Object( List<Location>) before it gets returned ??
  43.     //I started with this much below
  44.      List<Location> locations =  (from row in ds.Tables[0].AsEnumerable()
  45.                select new PropertyLocation
  46.                    {
  47.                    Name = row.Field<String>("Name"),
  48.                    Buildings = ?????
  49.                    }).ToList();
  50.  
  51.        return  locations;
  52.        }
  53. }
In above LoadPropertyInfo() method, how to cast DataSet to List<Location> locations before returning "locations" ? Pls help. Thanks!
Dec 17 '14 #1
2 1626
Actually, I'm not that good in the dataset, but I think this website http://msdn.microsoft.com/en-us/magazine/cc163877.aspx can help you a little to solve your problem. Just try it, there's no harm in trying.
Dec 18 '14 #2
Joseph Martell
198 Expert 128KB
Remember that XML Serialization is meant as a shortcut so you don't have to write as much code by hand to store and retrieve your objects from files. In this case you can actually bypass the data set completely and just put your xml directly into your objects.

First, you have to fix an issue with your XML Serialization attributes. According to your XML file you are including an array of buildings in each location so Location should look like this:

Expand|Select|Wrap|Line Numbers
  1.     public class Location {
  2.         [XmlAttribute("name")]
  3.         public string Name { get; set; }
  4.  
  5.         [XmlArray("Buildings")]
  6.         [XmlArrayItem("Building")]
  7.         public List<Building> Buildings { get; set; }
  8.     }
  9.  
Then your deserialization code gets dramatically simpler:

Expand|Select|Wrap|Line Numbers
  1.         public List<Location> LoadPropertyInfo() {
  2.             XmlSerializer  xmlSerializer = new XmlSerializer(typeof(PropertyInformation));
  3.             FileStream readStream = new FileStream("Example.xml", FileMode.Open);
  4.  
  5.             PropertyInformation pi = (PropertyInformation)xmlSerializer.Deserialize(readStream);    
  6.             readStream.Close();
  7.  
  8.             return pi.Locations;
  9.         }
  10.  
This code can be improved with better error handling, but for an example it gets the job done.
Jan 16 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bill Cohagan | last post by:
I'm trying to figure out the "best" way to implement a strongly typed ArrayList. Using inheritance is one approach. It has the advantage that I only have to write overrides for the Add method and...
0
by: | last post by:
We have an object factory that returns weakly typed datasets (just plain ol' DataSet's). I need to cast it, or transform it, or map it to a strongly typed dataset. There's an old adage I heard...
2
by: Mark | last post by:
Just wanted to confirm that my understanding of a strongly typed language is correct: 1. .NET is a strongly typed language because each variable / field must be declared a specific type (String,...
2
by: Oscar Thornell | last post by:
Hi, I have a general all purpose class that can take the name of a stored procedure and return a dataset... This general dataset that is returned I wish to cast to a strongly typed dataset of...
1
by: Andre Ranieri | last post by:
I'm designing an extranet web site that customers will use to log in and see their account history, make payments against their balance, etc. I've declared a strongly typed DataSet as a public...
20
by: Dennis | last post by:
I use the following code for a strongly typed arraylist and it works great. However, I was wondering if this is the proper way to do it. I realize that if I want to implement sorting of the...
1
by: rival | last post by:
Hi. I've a single solution with many projects. I'm using VS2003 and framework 1.1 1. Data project - contains UserData class, inheriting from DataSet, and setting up constants and columns...
3
by: Simon Hart | last post by:
Hi, I am trying to implement some functionality as seen in MS CRM 3.0 whereby a basic Xml is deserialized into an object which contains properties. What I want to do from here is; cast the basic...
1
by: Code Monkey | last post by:
Silly question maybe, but I've been doing the following for far too long now: public static DataTable myDataTable() { string sql = @"SELECT column1, column2, column3, column4 FROM...
4
by: Rachana | last post by:
Hi, I have understood Data Sets but what is meant by typed/untyped/ strongly typed datasets. Can any one explain me or suggest any site/ article, to get these concepts (and their ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.