如何设计和装饰类,以便我可以反序列化以下xml?

时间:2015-11-03 12:58:38

标签: c# xml xml-deserialization

我想将以下xml反序列化为自定义类:



<?xml version="1.0" encoding="ISO-8859-1"?>
<rss version="2.0">
<channel>
	<title>test</title>
	<description>test</description>
	<language>nl</language>
	<link>http://www.test.test</link>
	<item>
		<link>http://www.testlink1</link>
		<pubDate>Di, 03 Nov 2015 09:08:44 +0100</pubDate>
		<sector>
			<entry>a:b</entry>
			<entry>a:c</entry>
			<entry>a:d</entry>
			<entry>a:b</entry>
		</sector>
		<title>test</title>
		<lead><![CDATA[test]]></lead>
		<description><![CDATA[test	]]></description>
		<images>
			<enclosure url="http://test.dll?tem=LTO_IMAGE_DOC&amp;size=1024&amp;doc_id=100782" length="608055" type="image/jpg"/>
		</images>
	</item>
</channel>
</rss>
&#13;
&#13;
&#13;

这是我的班级:

    [Serializable]
    [XmlRoot("item", IsNullable = false)]
    public class CustomRssItem : BaseRssItem
    {
        [XmlElement("publisher", Namespace = "http://purl.org/dc/elements/1.1/")]
        public string Author { get; set; }

        [XmlElement("guid", Namespace = "")]
        public string Guid { get; set; }

        [XmlArray("sector")]
        [XmlArrayItem("entry")]
        public List<string> Sector { get; set; }

    }

如何修改我的类以使用自定义类而不是List<string>,以便我可以在entry元素上包含解析(将字符串拆分为冒号)?

而不是List<string>扇区我要写List<GroupEntry>扇区,其中GroupEntry将是我的自定义类,包含值&#34; a:b&#34;我可以解析和公开像Group和Subgroup这样的新属性吗?

2 个答案:

答案 0 :(得分:1)

通过使用正确的XmlSerialization属性实现GroupEntry类,我们可以实现该目标。

该类将包含a和b字段,但您不希望序列化这些字段,因此我们在序列化期间使用XmlIgnore属性忽略它们

然后我们有一个ab字段,我们想要将其序列化为元素文本。我们通过使用XmlText属性来实现这一点。

我们不仅实现了getter而且还实现了set的setter,因为当你从xml反序列化这种类型的实例时,这个setter是XmlSerialization会调用的。

以下是必需的GroupEntry类代码:

public class GroupEntry
{
    [System.Xml.Serialization.XmlIgnore]
    public string a;
    [System.Xml.Serialization.XmlIgnore]
    public string b;

    [System.Xml.Serialization.XmlText]
    public string ab
    {
        get
        {
            return string.Format("{0}:{1}", a, b);
        }
        set
        {
            a = null;
            b = null;
            if (value != null)
            {
                string[] split = value.Split(':');
                a = split[0];
                if (split.Length > 1)
                {
                    b = split[1];
                }
            }
        }
    }
}

答案 1 :(得分:0)

这就是我的建议

  1. 从XML文件XML to XSD
  2. 创建XSD
  3. 从XSD Create Class获取XSD to CS
  4. 以下是生成的类,现在基于此类,您可以调整您的类或按原样使用下面的类。您还可以通过使用适当的属性对属性进行装饰来重命名属性并自定义所有名称。

    //------------------------------------------------------------------------------
    // <auto-generated>
    //     This code was generated by a tool.
    //     Runtime Version:4.0.30319.42000
    //
    //     Changes to this file may cause incorrect behavior and will be lost if
    //     the code is regenerated.
    // </auto-generated>
    //------------------------------------------------------------------------------
    
    using System.Xml.Serialization;
    
    // 
    // This source code was auto-generated by xsd, Version=4.6.81.0.
    // 
    
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace="", IsNullable=false)]
    public partial class rss {
    
        private rssChannel channelField;
    
        private decimal versionField;
    
        /// <remarks/>
        public rssChannel channel {
            get {
                return this.channelField;
            }
            set {
                this.channelField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public decimal version {
            get {
                return this.versionField;
            }
            set {
                this.versionField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class rssChannel {
    
        private string titleField;
    
        private string descriptionField;
    
        private string languageField;
    
        private string linkField;
    
        private rssChannelItem itemField;
    
        /// <remarks/>
        public string title {
            get {
                return this.titleField;
            }
            set {
                this.titleField = value;
            }
        }
    
        /// <remarks/>
        public string description {
            get {
                return this.descriptionField;
            }
            set {
                this.descriptionField = value;
            }
        }
    
        /// <remarks/>
        public string language {
            get {
                return this.languageField;
            }
            set {
                this.languageField = value;
            }
        }
    
        /// <remarks/>
        public string link {
            get {
                return this.linkField;
            }
            set {
                this.linkField = value;
            }
        }
    
        /// <remarks/>
        public rssChannelItem item {
            get {
                return this.itemField;
            }
            set {
                this.itemField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class rssChannelItem {
    
        private string linkField;
    
        private string pubDateField;
    
        private string[] sectorField;
    
        private string titleField;
    
        private string leadField;
    
        private string descriptionField;
    
        private rssChannelItemImages imagesField;
    
        /// <remarks/>
        public string link {
            get {
                return this.linkField;
            }
            set {
                this.linkField = value;
            }
        }
    
        /// <remarks/>
        public string pubDate {
            get {
                return this.pubDateField;
            }
            set {
                this.pubDateField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlArrayItemAttribute("entry", IsNullable=false)]
        public string[] sector {
            get {
                return this.sectorField;
            }
            set {
                this.sectorField = value;
            }
        }
    
        /// <remarks/>
        public string title {
            get {
                return this.titleField;
            }
            set {
                this.titleField = value;
            }
        }
    
        /// <remarks/>
        public string lead {
            get {
                return this.leadField;
            }
            set {
                this.leadField = value;
            }
        }
    
        /// <remarks/>
        public string description {
            get {
                return this.descriptionField;
            }
            set {
                this.descriptionField = value;
            }
        }
    
        /// <remarks/>
        public rssChannelItemImages images {
            get {
                return this.imagesField;
            }
            set {
                this.imagesField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class rssChannelItemImages {
    
        private rssChannelItemImagesEnclosure enclosureField;
    
        /// <remarks/>
        public rssChannelItemImagesEnclosure enclosure {
            get {
                return this.enclosureField;
            }
            set {
                this.enclosureField = value;
            }
        }
    }
    
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.81.0")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
    public partial class rssChannelItemImagesEnclosure {
    
        private string urlField;
    
        private uint lengthField;
    
        private string typeField;
    
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string url {
            get {
                return this.urlField;
            }
            set {
                this.urlField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public uint length {
            get {
                return this.lengthField;
            }
            set {
                this.lengthField = value;
            }
        }
    
        /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public string type {
            get {
                return this.typeField;
            }
            set {
                this.typeField = value;
            }
        }
    }