1

我正在查询一个返回 XML 响应的 API,所以我一直在研究Controlling XML Serialization Using Attributes

API 响应如下所示: 在此处输入图像描述 我要做的是获取所有 CampaignDTO 元素 (0..*) 并将它们放在一个列表中。怎么可能做到这一点?由于最后的 Totalcount 元素,我一直遇到错误。

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
    }
4

1 回答 1

3

必须正确定义xml对应的对象模型。基于上面的示例 xml,我想出了下面的模型

[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
}

现在您可以从 xml 构造对象,如下所示

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
}

希望这可以帮助。

于 2012-04-18T13:57:04.973 回答