生成 XML 文档时出错。使用 XmlInclude 或 SoapInclude 特性静态指定非已知的类型。

前端之家收集整理的这篇文章主要介绍了生成 XML 文档时出错。使用 XmlInclude 或 SoapInclude 特性静态指定非已知的类型。前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Xml;
  8. using System.Xml.Serialization;
  9.  
  10. namespace ConsoleApp6
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. string r = string.Empty;
  17. List<Base> list = new List<Base>() {
  18. new A() { Name = "initName",AName = "aa" },new B() { Name = "initName",BName = "bb" }
  19. };
  20. try
  21. {
  22. r = XmlHelper.SerializeToXml(list);
  23. }
  24. catch (Exception ex)
  25. {
  26. /* 生成 XML 文档时出错。 */
  27. Console.WriteLine(ex.Message);
  28. /* 不应是类型 ConsoleApp6.A。使用 XmlInclude 或 SoapInclude 特性静态指定非已知的类型。 */
  29. Console.WriteLine(ex.InnerException.Message);
  30. }
  31.  
  32. Console.WriteLine(r);
  33. Console.Read();
  34. }
  35. }
  36.  
  37. #region [ Test Class ]
  38. //处理方法:在基类上加上所有需要序列化的子类的 XmlInclude 特性就可以了。
  39. //下面的代码去掉注释,即可正常运行
  40. /*
  41. [XmlInclude(typeof(A))]
  42. [XmlInclude(typeof(B))]
  43. */
  44. public class Base
  45. {
  46. public string Name { get; set; }
  47. }
  48.  
  49. public class A : Base
  50. {
  51. public string AName { get; set; }
  52. }
  53.  
  54. public class B : Base
  55. {
  56. public string BName { get; set; }
  57. }
  58. #endregion
  59.  
  60. #region [ XMLHelper ]
  61. public static class XmlHelper
  62. {
  63. public static void ObjectWriteToXmlFile<T>(T myObject,string filePath)
  64. {
  65. string xmlString = SerializeToXml<T>(myObject);
  66. File.WriteAllText(filePath,xmlString);
  67. }
  68.  
  69. public static T XmlFileToObject<T>(string filePath)
  70. {
  71. string xmlString = File.ReadAllText(filePath);
  72. return DeserializeToObject<T>(xmlString);
  73. }
  74.  
  75. /// <summary>
  76. /// 将自定义对象序列化为XML字符串
  77. /// </summary>
  78. /// <param name="myObject">自定义对象实体</param>
  79. /// <returns>序列化后的XML字符串</returns>
  80. public static string SerializeToXml<T>(T myObject)
  81. {
  82. if (myObject != null)
  83. {
  84. XmlSerializer xs = new XmlSerializer(typeof(T));
  85.  
  86. MemoryStream stream = new MemoryStream();
  87. XmlTextWriter writer = new XmlTextWriter(stream,Encoding.UTF8);
  88. writer.Formatting = Formatting.None;//缩进
  89. xs.Serialize(writer,myObject);
  90.  
  91. stream.Position = 0;
  92. StringBuilder sb = new StringBuilder();
  93. using (StreamReader reader = new StreamReader(stream,Encoding.UTF8))
  94. {
  95. string line;
  96. while ((line = reader.ReadLine()) != null)
  97. {
  98. sb.Append(line);
  99. }
  100. reader.Close();
  101. }
  102. writer.Close();
  103. return sb.ToString();
  104. }
  105. return string.Empty;
  106. }
  107.  
  108. /// <summary>
  109. /// 将XML字符串反序列化为对象
  110. /// </summary>
  111. /// <typeparam name="T">对象类型</typeparam>
  112. /// <param name="xml">XML字符</param>
  113. /// <returns></returns>
  114. public static T DeserializeToObject<T>(string xml)
  115. {
  116. T myObject;
  117. XmlSerializer serializer = new XmlSerializer(typeof(T));
  118. StringReader reader = new StringReader(xml);
  119. myObject = (T)serializer.Deserialize(reader);
  120. reader.Close();
  121. return myObject;
  122. }
  123. }
  124. #endregion
  125. }

猜你在找的XML相关文章