Advertisement
Guest User

Untitled

a guest
Mar 26th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. public class Person {
  2. [XmlElement]
  3. public int Id { get; set; }
  4. [XmlElement]
  5. public string Name { get; set; }
  6. }
  7. //=========
  8. XmlSerializer formatter = new XmlSerializer(typeof(List<Person>));
  9.  
  10. List<Person> personList = new List<Person>();
  11. personList.Add(new Person { Id = 5, Name = "Bob" });
  12. personList.Add(new Person { Id = 15, Name = "Tom" });
  13. Person kate = new Person { Id = 115, Name = "Kate" };
  14. personList.Add(kate);
  15.  
  16. string filePath = @"C:DELEteME.xml";
  17.  
  18. using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) {
  19. formatter.Serialize(fs, personList);
  20. }
  21.  
  22. <?xml version="1.0"?>
  23. <ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  24. <Person>
  25. <Id>5</Id>
  26. <Name>Bob</Name>
  27. </Person>
  28. <Person>
  29. <Id>15</Id>
  30. <Name>Tom</Name>
  31. </Person>
  32. <Person>
  33. <Id>115</Id>
  34. <Name>Kate</Name>
  35. </Person>
  36. </ArrayOfPerson>
  37.  
  38. personList.Remove(kate);
  39. using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate)) {
  40. formatter.Serialize(fs, personList);
  41. }
  42.  
  43. <?xml version="1.0"?>
  44. <ArrayOfPerson xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  45. <Person>
  46. <Id>5</Id>
  47. <Name>Bob</Name>
  48. </Person>
  49. <Person>
  50. <Id>15</Id>
  51. <Name>Tom</Name>
  52. </Person>
  53. </ArrayOfPerson><Id>115</Id>
  54. <Name>Kate</Name>
  55. </Person>
  56. </ArrayOfPerson>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement