反序列化XML文件属性

问题描述 投票:0回答:1

我正在尝试反序列化以下xml文件:

   <item>
    <title>Title</title>
    <link>https://blog.sitename.com/link</link>         <comments>https://blog.sitename.com/link/#respond</comments>
    <dc:creator><![CDATA[name]]></dc:creator>
    <pubDate>Thu, 30 Apr 2020 18:59:06 +0000</pubDate>
<description><p>description...</p><p> continue</p></description>
    <category><![CDATA[General News]]></category>
    <media:content url="https://blog.gotproperty.co.za/wp-content/uploads/2020/04/image.jpg" medium="image" />
    </item>

我的课程在下面

[XmlRoot("item")]
      public class item
        {
            public string title { get; set; }
            public string link { get; set; }
            public string comments{ get; set; }

            [XmlElement("media:content")]
            public Image Image { get; set; }

            public string description { get; set; }
            public string shortDescription
            {
                get
                {
                    var decodeHtml = HttpUtility.HtmlDecode(description);

                    var result = decodeHtml.Substring(decodeHtml.IndexOf('>') +1, decodeHtml.IndexOf("</") - decodeHtml.IndexOf('>') -1);

                    return result;
                }
            }
        }

        public class Image
        {
            [XmlAttribute("url")]
            public string url { get; set; }
            [XmlAttribute("medium")]
            public string medium { get; set; }

        }

除“ media:content”元素外,所有内容都在解析,该元素使用Image类,其中,URL和媒介定义为XmlAttributes。

c# .net-core xml-parsing
1个回答
0
投票

<media:content>不是xml元素名称;那是<content>,但是在命名空间中,无论您引用什么别名xmlns:media;因此,如果您有xmlns:media="http://foo/bar",则为:

[XmlElement("content", Namespace = "http://foo/bar")]
public Image Image { get; set; }
© www.soinside.com 2019 - 2024. All rights reserved.