fork download
  1. using System;
  2.  
  3. public class User
  4. {
  5. public string id;
  6. public string pass;
  7. }
  8.  
  9. public class Test
  10. {
  11. public static void Main()
  12. {
  13. string target = @"D:\test.xml";
  14. List<User> user = new List<User>();
  15. acclist.LoadXml(target);
  16.  
  17. // your code goes here
  18. }
  19. }
  20.  
  21. public class ListEx<Type> : System.Collections.Generic.List<Type>
  22. {
  23. // データ保存用のジェネリッククラス
  24. public class Xmldata<Type>
  25. {
  26. public Type[] data;
  27. }
  28.  
  29. // Xmlからコレクションを復元
  30. public void LoadXml(string path)
  31. {
  32. // コレクションをクリア
  33. this.Clear();
  34.  
  35. Xmldata<Type> buff = null;
  36. try
  37. {
  38. System.Xml.XmlDocument xdoc = new System.Xml.XmlDocument();
  39. System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(Xmldata<Type>));
  40.  
  41. xdoc.PreserveWhitespace = true;
  42. xdoc.Load(path);
  43.  
  44. System.Xml.XmlNodeReader xnr = new System.Xml.XmlNodeReader(xdoc.DocumentElement);
  45.  
  46. buff = (Xmldata<Type>)xml.Deserialize(xnr);
  47.  
  48. for (int i = 0; i < buff.data.Length; i++)
  49. {
  50. this.Add(buff.data[i]);
  51. }
  52. }
  53.  
  54. #region 例外処理
  55. catch
  56. {
  57.  
  58. }
  59. #endregion
  60.  
  61. return;
  62. }
  63.  
  64. // これでXmlシリアライズしたものを拡張メソッドのLoadXmlで読もうとした
  65. public void SaveXml(string path)
  66. {
  67. Xmldata<Type> xmldata = new Xmldata<Type>();
  68.  
  69. xmldata.data = this.ToArray();
  70.  
  71. try
  72. {
  73. using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.Create))
  74. {
  75. System.Xml.Serialization.XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(Xmldata<Type>));
  76.  
  77. // xmlファイルに書き込み
  78. xml.Serialize(fs, xmldata);
  79. }
  80. }
  81.  
  82. #region 例外処理
  83. catch
  84. {
  85.  
  86. }
  87. #endregion
  88.  
  89. return;
  90. }
  91. }
  92.  
  93. static class Linq
  94. {
  95. public class XmlData<Type> { public Type[] data; }
  96.  
  97. public static void LoadXml<Type>(this List<Type> list, string path)
  98. {
  99. list.Clear();
  100.  
  101. XmlData<Type> xmldata = null;
  102.  
  103. XmlDocument xdoc = new System.Xml.XmlDocument();
  104. XmlSerializer xml = new System.Xml.Serialization.XmlSerializer(typeof(XmlData<Type>));
  105.  
  106. xdoc.PreserveWhitespace = true;
  107. xdoc.Load(path);
  108.  
  109. XmlNodeReader xnr = new System.Xml.XmlNodeReader(xdoc.DocumentElement);
  110.  
  111. xmldata = (XmlData<Type>)xml.Deserialize(xnr);
  112.  
  113. for (int i = 0; i < xmldata.data.Length; i++)
  114. list.Add(xmldata.data[i]);
  115. }
  116.  
  117. public static void SaveXml<Type>(this List<Type> list, string path)
  118. {
  119. XmlData<Type> xmldata = new XmlData<Type>();
  120. xmldata.data = list.ToArray();
  121.  
  122. using (FileStream fs = new FileStream(path, FileMode.Create))
  123. {
  124. XmlSerializer xml = new XmlSerializer(typeof(XmlData<Type>));
  125.  
  126. xml.Serialize(fs, xmldata);
  127. }
  128. }
  129.  
  130. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cs(24,34): warning CS0693: Type parameter `Type' has the same name as the type parameter from outer type `ListEx<Type>'
prog.cs(21,27): (Location of the symbol related to previous warning)
prog.cs(97,51): error CS0246: The type or namespace name `List' could not be found. Are you missing `System.Collections.Generic' using directive?
prog.cs(117,51): error CS0246: The type or namespace name `List' could not be found. Are you missing `System.Collections.Generic' using directive?
Compilation failed: 2 error(s), 1 warnings
stdout
Standard output is empty