Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections.ObjectModel;
- using System.IO;
- using System.Xml.Serialization;
- namespace SerialiseCollection
- {
- class Program
- {
- static void Main(string[] args)
- {
- var c = new MyCollection();
- c.Add("Hello");
- c.Add("Goodbye");
- var serializer = new XmlSerializer(typeof(MyCollection));
- using (var writer = new StreamWriter("test.xml"))
- serializer.Serialize(writer, c);
- }
- }
- [XmlRoot("MyCollection")]
- public class MyCollection : Collection<string>
- {
- [XmlAttribute()]
- public string MyAttribute { get; set; }
- public MyCollection()
- {
- this.MyAttribute = "SerializeThis";
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <MyCollection xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <string>Hello</string>
- <string>Goodbye</string>
- </MyCollection>
- <MyCollection MyAttribute="SerializeThis"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <string>Hello</string>
- <string>Goodbye</string>
- </MyCollection>
- [Serializable]
- public class MyCollectionWrapper {
- [XmlAttribute]
- public string SomeProp {get;set;} // custom props etc
- [XmlAttribute]
- public int SomeOtherProp {get;set;} // custom props etc
- public Collection<string> Items {get;set;} // the items
- }
- <MyCollection MyAttribute="SerializeThis"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <Items>
- <string>Hello</string>
- <string>Goodbye</string>
- <Items>
- </MyCollection>
- <MyCollection MyAttribute="SerializeThis"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <string>Hello</string>
- <string>Goodbye</string>
- </MyCollection>
Add Comment
Please, Sign In to add comment