0

I want to add an Xml Attribute to my class property that is XmlElement:

   [XmlRoot(ElementName = "item")]
    public class Item
    {
        [XmlElement(ElementName = "title")]
        public string Title { get; set; }
        [XmlElement(ElementName = "description")]
        public string Description { get; set; }
        [XmlElement(ElementName = "link")]
        public string Link { get; set; } //content webpage link
        [XmlElement(ElementName = "pubdate")]
        public string PubDate { get; set; }
        [XmlElement(ElementName = "valid")]
        public string Valid { get; set; }
        [XmlElement(ElementName = "videoAssertId")]
        ***//ADD here an XmlAttribute with value to this element (videoAssertId)***
        public string VideoAssertId { get; set; }
        [XmlElement(ElementName = "durableAppId")]
        public string DuarableAppId { get; set; } 
        [XmlElement(ElementName = "seriesAssertId")]
        public string SerieAssertId { get; set; } 
    }
}

RESULT SHOULD BE

<item>
        <title>xxxxxx</title>
        <description>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.</description>
        <link>https://link/</link>
        <pubdate>2002-10-13T09:00Z</pubdate>
        <valid>start=2002-10-13T09:00Z;end=2002-10-17T17:00Z;scheme=W3C-DTF</valid>
        <videoAssertId **provider=xxx**>5241112554</videoAssertId>
        <durableAppId>xxxxxxxx</durableAppId>
      </item>

The attribute 'provider' should appear in 'VideoAssertId' element. Some one know how todo that please, I'm loosing my mind with that?

  • One approach (though not necessarily the cleanest, as evidenced by comments on the answer) is to [use a wrapper class](https://stackoverflow.com/a/11330786/3791245) as the type for the property `VideoAssertId`, rather than using `string` directly. In a way this makes sense, though: you seem to want two values represented by `VideoAssertId` - both the actual value, and the provider string. – Sean Skelly Mar 04 '22 at 23:33
  • Thanks Sean for your answer, that right I did it at first time, but I should respect an specific Xml schema (this solution will create an sub node in the Xml, not just an element with attribute) 5241112554 – FAYCAL KADRI Mar 07 '22 at 14:20

0 Answers0