Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Person {
- [XmlElement]
- public int Id { get; set; }
- [XmlElement]
- public string Name { get; set; }
- }
- //=========
- XmlSerializer formatter = new XmlSerializer(typeof(List<Person>));
- List<Person> personList = new List<Person>();
- personList.Add(new Person { Id = 5, Name = "Bob" });
- personList.Add(new Person { Id = 15, Name = "Tom" });
- Person kate = new Person { Id = 115, Name = "Kate" };
- personList.Add(kate);
- string filePath = @"C:DELEteME.xml";
- using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) {
- formatter.Serialize(fs, personList);
- }
- <?xml version="1.0"?>
- <ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <Person>
- <Id>5</Id>
- <Name>Bob</Name>
- </Person>
- <Person>
- <Id>15</Id>
- <Name>Tom</Name>
- </Person>
- <Person>
- <Id>115</Id>
- <Name>Kate</Name>
- </Person>
- </ArrayOfPerson>
- personList.Remove(kate);
- using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) {
- formatter.Serialize(fs, personList);
- }
- <?xml version="1.0"?>
- <ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <Person>
- <Id>5</Id>
- <Name>Bob</Name>
- </Person>
- <Person>
- <Id>15</Id>
- <Name>Tom</Name>
- </Person>
- </ArrayOfPerson><Id>115</Id>
- <Name>Kate</Name>
- </Person>
- </ArrayOfPerson>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement