473,405 Members | 2,445 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,405 software developers and data experts.

C# class to Deserialize XML to...

I am trying to Deserialize an xml to a C# object so i can use it efficently. I have done this with much less complex xml documents so i gave it a shot and it fails on Deserialize call with no good information in the error to track it down. i will first post the xml followed by the class i wrote which fails. please shed some light if you can...

xml:
Expand|Select|Wrap|Line Numbers
  1. <sitesResponse xmlns:gml="http://www.opengis.net/gml" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:wtr="http://www.cuahsi.org/waterML/" xmlns="http://www.cuahsi.org/waterML/1.0/">
  2.   <queryInfo>
  3.     <criteria />
  4.   </queryInfo>
  5.   <site>
  6.     <siteInfo>
  7.       <siteName>Owasco Lake Buoy</siteName>
  8.       <siteCode network="FLOWEN" siteID="1">Buoy001</siteCode>
  9.       <geoLocation>
  10.         <geogLocation xsi:type="LatLonPointType" srs="EPSG:26918">
  11.           <latitude>42.853289</latitude>
  12.           <longitude>-76.516628</longitude>
  13.         </geogLocation>
  14.       </geoLocation>
  15.       <verticalDatum>NAVD88</verticalDatum>
  16.       <note title="County">Cayuga</note>
  17.       <note title="State">New York</note>
  18.     </siteInfo>
  19.   </site>
  20.   <site>
  21.     <siteInfo>
  22.       <siteName>OwascoInlet</siteName>
  23.       <siteCode network="FLOWEN" siteID="2">Inlet001</siteCode>
  24.       <geoLocation>
  25.         <geogLocation xsi:type="LatLonPointType" srs="EPSG:26918">
  26.           <latitude>42.716106</latitude>
  27.           <longitude>-76.437281</longitude>
  28.         </geogLocation>
  29.       </geoLocation>
  30.       <note title="County">Cayuga</note>
  31.       <note title="State">New York</note>
  32.     </siteInfo>
  33.   </site>
  34.   <site>
  35.     <siteInfo>
  36.       <siteName>DutchHollowBrook</siteName>
  37.       <siteCode network="FLOWEN" siteID="3">DHB001</siteCode>
  38.       <geoLocation>
  39.         <geogLocation xsi:type="LatLonPointType" srs="EPSG:26918">
  40.           <latitude>42.862406</latitude>
  41.           <longitude>-76.510892</longitude>
  42.         </geogLocation>
  43.       </geoLocation>
  44.       <note title="County">Cayuga</note>
  45.       <note title="State">New York</note>
  46.     </siteInfo>
  47.   </site>
  48.   <site>
  49.     <siteInfo>
  50.       <siteName>IAGT Weather Station</siteName>
  51.       <siteCode network="FLOWEN" siteID="4">CAYUMet</siteCode>
  52.       <geoLocation>
  53.         <geogLocation xsi:type="LatLonPointType" srs="EPSG:26918">
  54.           <latitude>42.943528</latitude>
  55.           <longitude>-76.540317</longitude>
  56.         </geogLocation>
  57.       </geoLocation>
  58.       <note title="County">Cayuga</note>
  59.       <note title="State">New York</note>
  60.     </siteInfo>
  61.   </site>
  62. </sitesResponse>
  63.  
Class:
Expand|Select|Wrap|Line Numbers
  1.  
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Collections;
  12. using System.IO;
  13. using System.Xml;
  14. using System.Xml.Serialization;
  15.  
  16. /// <summary>
  17. /// Summary description for ODMSiteList
  18. /// </summary>
  19. [XmlRoot("sitesResponse")]
  20. public class ODMSiteList
  21. {
  22.     private ArrayList siteList;
  23.     public ODMSiteList()
  24.     {
  25.         siteList = new ArrayList();
  26.     }
  27.     [XmlElement("queryInfo")]
  28.     public ODMQueryInfo query = new ODMQueryInfo();
  29.     [XmlElement("site")]
  30.     public ODMSite[] sites
  31.     {
  32.         get
  33.         {
  34.             ODMSite[] sites = new ODMSite[siteList.Count];
  35.             siteList.CopyTo(sites);
  36.             return sites;
  37.         }
  38.         set
  39.         {
  40.             if (value == null) return;
  41.             ODMSite[] sites = (ODMSite[])value;
  42.             siteList.Clear();
  43.             foreach (ODMSite site in sites)
  44.                 siteList.Add(site);
  45.         }
  46.     }
  47.     public int AddItem(Site site)
  48.     {
  49.         return siteList.Add(site);
  50.     }
  51. }
  52. public class ODMSite
  53. {
  54.     [XmlElement("siteinfo")]
  55.     private ODMSiteInfo siteInfoList;
  56.     public ODMSite()
  57.     {
  58.         siteInfoList = new ODMSiteInfo();
  59.     }
  60. }
  61.  
  62. public class ODMSiteInfo
  63. {
  64.     [XmlElement("sitename")]
  65.     public string SiteName;
  66.     [XmlElement("sitecode")]
  67.     public string SiteCode;
  68.     [XmlElement("geoLocation")]
  69.     public ODMGeoLocation odmGeoLoc;
  70.     [XmlElement("verticalDatum")]
  71.     public string verticalDatum;
  72.     [XmlElement("note")]
  73.     ODMNote[] note;
  74.  
  75.     public ODMSiteInfo()
  76.     {
  77.         note = new ODMNote();
  78.     }
  79.  
  80. }
  81. public class ODMGeoLocation
  82. {
  83.     [XmlElement("geogLocation")]
  84.     ODMGeogLocation odmgeog;
  85.     public ODMGeoLocation()
  86.     {
  87.         odmgeog = new ODMGeogLocation();
  88.     }
  89. }
  90. public class ODMGeogLocation
  91. {
  92.     [XmlAttribute("type")]
  93.     public string geogType;
  94.     [XmlAttribute("srs")]
  95.     public string geogSrs;
  96.     [XmlElement("Latitude")]
  97.     public float Latitude;
  98.     [XmlElement("Longitude")]
  99.     public float Longitude;
  100.  
  101.     public ODMGeogLocation()
  102.     {
  103.  
  104.     }
  105. }
  106. public class ODMNote
  107. {
  108.     [XmlAttribute("title")]
  109.     public string title;
  110.     public string note;
  111.     public ODMNote()
  112.     {
  113.  
  114.     }
  115. }
  116. public class ODMQueryInfo
  117. {
  118.     [XmlElement("criteria")]
  119.     public string criteria = string.Empty;
  120.     public ODMQueryInfo()
  121.     {
  122.  
  123.     }
  124. }
  125.  
Oct 9 '07 #1
3 3459
that was a fairly easy fix, i forgot to put the namespace in the class...
but now i seem to have an issue with the data getting in the fields, they all come back as null at the siteinfo level. The updated class now looks like:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Collections;
  11. using System.IO;
  12. using System.Xml;
  13. using System.Xml.Serialization;
  14.  
  15. /// <summary>
  16. /// Summary description for ODMSiteList
  17. /// </summary>
  18. /// 
  19.  
  20.  
  21.  
  22.  
  23. [XmlRoot("sitesResponse", Namespace="http://www.cuahsi.org/waterML/1.0/")]
  24. public class ODMSiteList
  25. {
  26.     private ArrayList siteList;
  27.     public ODMSiteList()
  28.     {
  29.         siteList = new ArrayList();
  30.     }
  31.     [XmlElement("queryInfo")]
  32.     public ODMQueryInfo query = new ODMQueryInfo();
  33.     [XmlElement("site")]
  34.     public ODMSite[] sites
  35.     {
  36.         get
  37.         {
  38.             ODMSite[] sites = new ODMSite[siteList.Count];
  39.             siteList.CopyTo(sites);
  40.             return sites;
  41.         }
  42.         set
  43.         {
  44.             if (value == null) return;
  45.             ODMSite[] sites = (ODMSite[])value;
  46.             siteList.Clear();
  47.             foreach (ODMSite site in sites)
  48.                 siteList.Add(site);
  49.         }
  50.     }
  51.     public int AddItem(Site site)
  52.     {
  53.         return siteList.Add(site);
  54.     }
  55. }
  56. public class ODMSite
  57. {
  58.     [XmlElement("siteinfo")]
  59.     private ODMSiteInfo siteInfoList;
  60.     public ODMSiteInfo SiteInfoList
  61.     {
  62.         get
  63.         {
  64.             return siteInfoList;
  65.         }
  66.         set
  67.         {
  68.             if (siteInfoList == value)
  69.                 return;
  70.             siteInfoList = value;
  71.         }
  72.     }
  73.     public ODMSite()
  74.     {
  75.         siteInfoList = new ODMSiteInfo();
  76.     }
  77. }
  78.  
  79. public class ODMSiteInfo
  80. {
  81.     [XmlElement("sitename")]
  82.     private string _SiteName;
  83.     public string SiteName
  84.     {
  85.         get { return _SiteName; }
  86.         set { _SiteName = value; }
  87.     }
  88.  
  89.     [XmlElement("sitecode")]
  90.     private string _SiteCode;
  91.     public string SiteCode
  92.     {
  93.         get { return _SiteCode; }
  94.         set { _SiteCode = value; }
  95.     }
  96.     [XmlElement("geoLocation")]
  97.     private ODMGeoLocation _OdmGeoLoc;
  98.     public ODMGeoLocation OdmGeoLoc
  99.     {
  100.         get { return _OdmGeoLoc; }
  101.         set { _OdmGeoLoc = value; }
  102.     }
  103.     [XmlElement("verticalDatum")]
  104.     private string _VerticalDatum;
  105.     public string VerticalDatum
  106.     {
  107.         get { return _VerticalDatum; }
  108.         set { _VerticalDatum = value; }
  109.     }
  110.     [XmlElement("note")]
  111.     private ArrayList _Note;
  112.     public ArrayList note
  113.     {
  114.         get { return _Note; }
  115.         set
  116.         {
  117.             _Note = value;
  118.         }
  119.     }
  120.  
  121.     public ODMSiteInfo()
  122.     {
  123.         note = new ArrayList();
  124.     }
  125.  
  126. }
  127. public class ODMGeoLocation
  128. {
  129.     [XmlElement("geogLocation")]
  130.     ODMGeogLocation odmgeog;
  131.     public ODMGeoLocation()
  132.     {
  133.         odmgeog = new ODMGeogLocation();
  134.     }
  135. }
  136. public class ODMGeogLocation
  137. {
  138.     [XmlAttribute("type")]
  139.     public string geogType;
  140.     [XmlAttribute("srs")]
  141.     public string geogSrs;
  142.     [XmlElement("Latitude")]
  143.     public float Latitude;
  144.     [XmlElement("Longitude")]
  145.     public float Longitude;
  146.  
  147.     public ODMGeogLocation()
  148.     {
  149.  
  150.     }
  151. }
  152. public class ODMNote
  153. {
  154.     [XmlAttribute("title")]
  155.     public string title;
  156.     public string note;
  157.     public ODMNote()
  158.     {
  159.  
  160.     }
  161. }
  162. public class ODMQueryInfo
  163. {
  164.     [XmlElement("criteria")]
  165.     public string criteria = string.Empty;
  166.     public ODMQueryInfo()
  167.     {
  168.  
  169.     }
  170. }
  171.  
Oct 9 '07 #2
RedSon
5,000 Expert 4TB
Billy,

You might want to post in the XML forum with a link to this thread and see if any of those guys can offer some assistance.
Oct 9 '07 #3
ok, thanx, i'll do.....
Oct 9 '07 #4

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

Similar topics

4
by: Bob Rock | last post by:
Hello, I've got an xml stream that I'd need to deserialize into an instance of a given class A. I'd like to create an instance method on class A (method Deserialize) that takes this XML stream...
1
by: Alex | last post by:
I am creating an application that allows the user to link a plug-in "utility" class (Watcher) to a class at runtime. There are several Watcher utilities, each with a different style of "watching"...
0
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
1
by: Mike Pollett | last post by:
Hi, I have used the ISerializable interface before and the code below worked fine. Until I derived it from CollectionBase. The code will still serialize and deserialize the properties in this class...
7
by: Just D. | last post by:
All, What's the easiest way to deserialize class from the class itself? Step by step. We create some class, add all required USINGs, make this class serializable, then add a method like: ...
2
by: Drolem | last post by:
Hello All, I use the following method to serialize a settings class to a string: '======================================================================= Public Shared Function...
0
by: a | last post by:
Hi, I am using a SoapFormatter to serialize a class which has a member that is derived from DictionaryBase. Everything works fine... I can serialize and deserialize. The trouble I have is...
0
by: Craig Buchanan | last post by:
i am trying to build an application that uses plugins to extend the business functionality of an application. i've decided to use inheritance of abstract classes as the mechanism to do this. ...
3
by: Phill W. | last post by:
OK, I've asked nicely before; now I'm going to throw down the gauntlet to anyone brave enough to take it up. In VB'2005, can anyone write me a class that inherits from System.Data.DataTable, add...
0
by: shapper | last post by:
Hello, I followed some examples in internet and in MSDN web site to create a serializable class. Basically, I need to serialize a class to binary so I can save it in an SQL 2005 table using a...
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
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.