Dofactory.com
Dofactory.com
 Back to list
Views:   12.6K
Replies:  3
Archived

How can I generate xml elements or attributes from a custom collection?

 Hi:

I have a custom collection and items have two properties. 
I want to serialize one property to an xmlelement and another property to an xmlattribute. How can I do this? 

Thanks.
Best Regards.
Volkan Genç, May 16, 2011
Reply 1
Wow! I have one of the most trending game that you loves to play online most of the kids loves to play fireboy and watergirl online game without any download and login the goal of the game is to collect the gems and run more to achieve the gems and avoid to pass through the red blue and green lake.
Himanshu Mishra, May 28, 2018
Reply 2

Give the [Serializable] attribute to appropriate property such a way you can serialize it by selective members.

using System.Xml.Serialization;

namespace SampleApp
{
    [Serializable][XmlRoot("MyCollection")]
    class CustomCollection
    {
        [XmlElement("myElement")]
        public string MyElementProperty { get; set; }

        [XmlAttribute("myAttribute")]
        public string MyAttributeProperty { get; set; }
    }
}


Thanks
Rajesh

Rajesh Shanmugam, Jun 01, 2011
Reply 3
To accomplish your goal add attributes to the properties in your collection. When the XML Serializer writes the XML it will create the requested node type.

Here's a sample in C#
using System.Xml.Serialization;

namespace SampleApp
{
    [XmlRoot("MyCollection")]
    class CustomCollection
    {
        [XmlElement("myElement")]
        public string MyElementProperty { get; set; }

        [XmlAttribute("myAttribute")]
        public string MyAttributeProperty { get; set; }
    }
}
Hope it helps,

Steve

 
Steve Thompson, May 17, 2011
Stay Inspired!
Join other developers and designers who have already signed up for our mailing list.
Terms     Privacy     Cookies       Do Not Sell       Licensing      
Made with    in Austin, Texas.  - vsn 44.0.0
© Data & Object Factory, LLC.