1

I'm querying an API which returns a response in XML, so I've been looking into Controlling XML Serialization Using Attributes .

The API response looks like this: enter image description here What I want to do is takes all the CampaignDTO elements (0..*) and put them in a list. How could this be done? I keep running into errors because of the Totalcount element at the end.

public class Campaign
    {
        #region CTor
        public Campaign()
        {
        }
        #endregion

        #region Properties

        [XmlElement(ElementName = "Id_campaign")]
        public string ID_Campaign { get; set; }
        [XmlElement(ElementName = "Campaignname")]
        public string ElementName { get; set; }
        [XmlElement(ElementName = "Websiteurl")]
        public string WebsiteUrl { get; set; }
        [XmlElement(ElementName = "Privacypolicyurl")]
        public string PrivacyPolicyUrl { get; set; }
        [XmlElement(ElementName = "Termsurl")]
        public string TermsUrl { get; set; }
        [XmlElement(ElementName = "Pricepageurl")]
        public string PricepageUrl { get; set; }
        [XmlElement(ElementName = "Maxcredit")]
        public Int32 MaxCredit { get; set; }
        [XmlElement(ElementName = "Fk_id_currency")]
        public string FK_ID_Currency { get; set; }
        [XmlElement(ElementName = "Maxscans")]
        public short MaxScans { get; set; }
        [XmlElement(ElementName = "Startdate")]
        public DateTime Startdate { get; set; }
        [XmlElement(ElementName = "Enddate")]
        public DateTime Enddate { get; set; }
        [XmlElement(ElementName = "Starthour")]
        public short Starthour { get; set; }
        [XmlElement(ElementName = "Endhour")]
        public short Endhour { get; set; }
        [XmlElement(ElementName = "Pmam")]
        public string PMAM { get; set; }
        [XmlElement(ElementName = "Language")]
        public string Language { get; set; }
        [XmlElement(ElementName = "Fk_id_merchantapp")]
        public string FK_ID_MerchantApp { get; set; }
        [XmlElement(ElementName = "Campaigntype")]
        public string CampaignType { get; set; }
        [XmlElement(ElementName = "Createtimestamp")]
        public DateTime CreateTimestamp { get; set; }
        [XmlElement(ElementName = "Lastupdate")]
        public DateTime LastUpdate { get; set; }
        [XmlElement(ElementName = "Lastupdateby")]
        public string LastUpdateBy { get; set; }
        [XmlElement(ElementName = "Status")]
        public short Status { get; set; }

        #endregion
    }
1
  • 1
    You would do better to post the actual XML, as XML, instead of posting a picture of it. Commented Apr 18, 2012 at 16:11

1 Answer 1

3

You must define the object model corresponding to the xml correctly. Based on the sample xml above, I've come up with the below model

[XmlRoot("CampaignListXml")]
public class CampaignList
{
    [XmlElement]
    public Allcampaign Allcampaign;

    [XmlElement]
    public int TotalCount;
}

public class Allcampaign
{
    [XmlElement("CompaignDTO", typeof(Campaign))]
    public Campaign[] CampaignArray;
}

public class Campaign
{
    #region CTor
    public Campaign()
    {
    }
    #endregion

    #region Properties

    [XmlElement(ElementName = "Id_campaign")]
    public string ID_Campaign { get; set; }
    [XmlElement(ElementName = "Campaignname")]
    public string ElementName { get; set; }
    [XmlElement(ElementName = "Websiteurl")]
    public string WebsiteUrl { get; set; }
    [XmlElement(ElementName = "Privacypolicyurl")]
    public string PrivacyPolicyUrl { get; set; }
    [XmlElement(ElementName = "Termsurl")]
    public string TermsUrl { get; set; }
    [XmlElement(ElementName = "Pricepageurl")]
    public string PricepageUrl { get; set; }
    [XmlElement(ElementName = "Maxcredit")]
    public Int32 MaxCredit { get; set; }
    [XmlElement(ElementName = "Fk_id_currency")]
    public string FK_ID_Currency { get; set; }
    [XmlElement(ElementName = "Maxscans")]
    public short MaxScans { get; set; }
    [XmlElement(ElementName = "Startdate")]
    public DateTime Startdate { get; set; }
    [XmlElement(ElementName = "Enddate")]
    public DateTime Enddate { get; set; }
    [XmlElement(ElementName = "Starthour")]
    public short Starthour { get; set; }
    [XmlElement(ElementName = "Endhour")]
    public short Endhour { get; set; }
    [XmlElement(ElementName = "Pmam")]
    public string PMAM { get; set; }
    [XmlElement(ElementName = "Language")]
    public string Language { get; set; }
    [XmlElement(ElementName = "Fk_id_merchantapp")]
    public string FK_ID_MerchantApp { get; set; }
    [XmlElement(ElementName = "Campaigntype")]
    public string CampaignType { get; set; }
    [XmlElement(ElementName = "Createtimestamp")]
    public DateTime CreateTimestamp { get; set; }
    [XmlElement(ElementName = "Lastupdate")]
    public DateTime LastUpdate { get; set; }
    [XmlElement(ElementName = "Lastupdateby")]
    public string LastUpdateBy { get; set; }
    [XmlElement(ElementName = "Status")]
    public short Status { get; set; }

    #endregion
}

Now you can construct the object from xml as below

using (StringReader reader = new StringReader(xml))
{
    XmlSerializer serializer = new XmlSerializer(typeof(CampaignList));
    CampaignList x1 = serializer.Deserialize(reader) as CampaignList;
    Compaign[] compaignArray = x1.Allcompaign.CompaignArray; //This will have all the compaign list
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.