Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / XML / September 2004

Tip: Looking for answers? Try searching our database.

XML Serialization

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Drakier Dominaeus - 29 Sep 2004 03:34 GMT
Hello all...

My problem is this.. I have an XML that has been formatted and given to me
already. My goal is to attempt to serialize the XML into an object using a
regular class and XMLSerializer if possible. I don't want to stroll too far
outside the realm of "normal". My main problem is that in my XML, I have a
node called "Referrals" which has both an array of Referral nodes under it
AS WELL AS a lot of attributes. I need to have both the attributes and all
array nodes accessable through my class. I am able to get either the
attributes OR the Array, but I cannot get both. I have something wrong, and
I am completely stumped.

Anyway... I will first post the XML I am trying to serialize and then I will
post the class that I'm using to attempt to serialize, and someone might be
able to help me out a little?

<User ID="1234567" Email="nospam@email.com" OfferComplete="0"
ReferralsComplete="0" Approved="0" Hold="0">

 <Orders OrderCount="1">
   <Order OrderNumber="1234567" OrderStatus="Shipped"
TrackingNumber="ZA1234567890"/>
 </Orders>

 <Referrals ReferralsRequired="5" Stage3Count="1" Stage2Count="1"
Stage1Count="1" Stage0Count="1">
   <Referral ToName="On Hold" Email="hold@email.com" ReferralStage="0"
ReferralStageText="Referral denied"/>
   <Referral ToName="Not Joined" Email="nojoin@email.com" ReferralStage="1"
ReferralStageText="Referral has not joined"/>
   <Referral ToName="Joined" Email="joined@email.com" ReferralStage="2"
ReferralStageText="Joined, but offer not completed"/>
   <Referral ToName="Completed" Email="complete@email.com"
ReferralStage="3" ReferralStageText="Offer Completed"/>
 </Referrals>

</User>

[XmlRootAttribute("User", Namespace="", IsNullable=false)]
public class User
{
 [XmlAttribute]
 public string ID;
 [XmlAttribute]
 public string Email;
 [XmlAttribute]
 public string OfferComplete;
 [XmlAttribute]
 public string ReferralsComplete;
 [XmlAttribute]
 public string Approved;
 [XmlAttribute]
 public string Hold;

 /* Uncomment this section to get the
      Array Attributes properly
    but not the Array Elements       */
 /*
 public clsReferrals Referrals;
 public class clsReferrals
 {
  [XmlAttribute]
  public string ReferralsRequired;
  [XmlAttribute]
  public string Stage3Count;
  [XmlAttribute]
  public string Stage2Count;
  [XmlAttribute]
  public string Stage1Count;
  [XmlAttribute]
  public string Stage0Count;

  [XmlArrayItem(ElementName="Referral")]
  public clsReferral[] Referrals;
  public class clsReferral
  {
   [XmlAttribute]
   public string ToName;
   [XmlAttribute]
   public string Email;
   [XmlAttribute]
   public string ReferralStage;
   [XmlAttribute]
   public string ReferralStageText;
  }
 }
 */

 /* Uncomment this section to get the
      Array Elements properly         */
 /*
 [XmlArrayItem(ElementName="Referral")]
 public clsReferral[] Referrals;
 public class clsReferral
 {
  [XmlAttribute]
  public string ToName;
  [XmlAttribute]
  public string Email;
  [XmlAttribute]
  public string ReferralStage;
  [XmlAttribute]
  public string ReferralStageText;
 }
 */
}
Drakier Dominaeus - 29 Sep 2004 22:13 GMT
Ok..

I finally got it after playing around with it... I thought I had tried this
before, but obviously not..

Anyway.. for anyone interested, the final class looked like this...

[XmlRootAttribute("User", Namespace="", IsNullable=false)]
public class User
{
 [XmlAttribute]
 public string ID;
 [XmlAttribute]
 public string Email;
 [XmlAttribute]
 public string OfferComplete;
 [XmlAttribute]
 public string ReferralsComplete;
 [XmlAttribute]
 public string Approved;
 [XmlAttribute]
 public string Hold;

 [XmlElement("Referrals")]
 public clsReferrals Referrals;
 public class clsReferrals
 {
  [XmlAttribute]
  public string ReferralsRequired;
  [XmlAttribute]
  public string Stage3Count;
  [XmlAttribute]
  public string Stage2Count;
  [XmlAttribute]
  public string Stage1Count;
  [XmlAttribute]
  public string Stage0Count;

  [XmlElement("Referral")]
  public clsReferral[] Referral;
  public class clsReferral
  {
   [XmlAttribute]
   public string ToName;
   [XmlAttribute]
   public string Email;
   [XmlAttribute]
   public string ReferralStage;
   [XmlAttribute]
   public string ReferralStageText;
  }
 }
}

> Hello all...
>
[quoted text clipped - 102 lines]
>  */
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.