【问题标题】:How to design and decorate class(es) so I can deserialize following xml?如何设计和装饰类,以便我可以反序列化以下 xml?
【发布时间】:2015-11-03 12:58:38
【问题描述】:

我想将以下 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>

这是我的课:

    [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&lt;string&gt;,以便我可以包含我需要对条目元素执行的解析(将字符串与冒号分开)?

我想写 List&lt;string&gt; Sector 而不是 List&lt;GroupEntry&gt; Sector,其中 GroupEntry 将是我的自定义类,其中包含值“a:b”,我可以解析和公开新属性,如 Group 和 Subgroup?

【问题讨论】:

  • 不清楚你在问什么。请更具体。
  • 创建一个GroupEntry 类?
  • 我确实创建了 GroupEntry 类,但它没有得到“填充”...
  • xsd 方法是我承认的最佳实践,但它可能会将 GroupEntry 类型生成为字符串而不是复杂类型,因为它无法知道我们试图实现的目标

标签: c# xml xml-deserialization


【解决方案1】:

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

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

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

我们不仅实现了 getter,还实现了 ab 的 setter,因为当您从 xml 反序列化这种类型的实例时,XmlSerialization 将调用这个 setter。

这是所需的 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];
                }
            }
        }
    }
}

【讨论】:

    【解决方案2】:

    这是我的建议

    1. 从 XML 文件 创建 XSD
    2. 来自 XSD Create Class 为它

    下面是生成的类,现在基于这个类你可以调整你的类或者使用下面的类。您还可以重命名属性并通过使用适当的属性装饰它们来自定义所有名称。

    //------------------------------------------------------------------------------
    // <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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多