all groups > dotnet xml > september 2004 >
You're in the

dotnet xml

group:

XML Serialization



XML Serialization Drakier Dominaeus
9/28/2004 7:34:52 PM
dotnet xml: 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;
}
*/
}

Re: XML Serialization Drakier Dominaeus
9/29/2004 2:13:13 PM
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;
}
}
}


[quoted text, click to view]

AddThis Social Bookmark Button