简体   繁体   中英

Deserializing XML File attributes

I'm trying to deserialize the following xml file:

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

My class is the following

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

        }

Everything is parsing except for the "media:content" element which uses the Image class where the url and medium is defined as XmlAttributes.

<media:content> is not an xml element name; that is <content> , but in the namespace by whatever you have an alias xmlns:media referring to; so if you have xmlns:media="http://foo/bar" , then this is:

[XmlElement("content", Namespace = "http://foo/bar")]
public Image Image { get; set; }

To be explicit: if you have xmlns:media="http://foo/bar" , then: <media:content> is similar to <content xmlns="http://foo/bar"> , except that the namespace "http://foo/bar" is not inherited by child elements.

Update on Marc Gravell's Answer.

Sorry didn't realize the Xml header is needed so I only posted a snipped of the Xml file.

Xml header

 <?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
        xmlns:content="http://purl.org/rss/1.0/modules/content/"
        xmlns:wfw="http://wellformedweb.org/CommentAPI/"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        xmlns:atom="http://www.w3.org/2005/Atom"
        xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
        xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
        xmlns:media="http://search.yahoo.com/mrss/"
    >

Class Update

[XmlElement("content", Namespace = "http://search.yahoo.com/mrss/")]
public Image Image { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM