C# 具有不同类的通用列表XML序列化
我有以下代码: BaseContent.csC# 具有不同类的通用列表XML序列化,c#,generics,xml-serialization,microsoft-metro,.net-4.5,C#,Generics,Xml Serialization,Microsoft Metro,.net 4.5,我有以下代码: BaseContent.cs public class BaseContent { // Some auto properties } public class News : BaseContent { // Some more auto properties } public class Event : BaseContent { // Some more auto properites } public class GenericResponse<
public class BaseContent
{
// Some auto properties
}
public class News : BaseContent
{
// Some more auto properties
}
public class Event : BaseContent
{
// Some more auto properites
}
public class GenericResponse<T>
{
[XmlArray("Content")]
[XmlArrayItem("NewsObject", typeof(News)]
[XmlArrayItem("EventObject", typeof(Event)]
public List<T> ContentItems { get; set; }
}
public class NewsResponse : GenericResponse<News> {}
public class EventResponse : GenericResponse<Event> {}
News.cs
public class BaseContent
{
// Some auto properties
}
public class News : BaseContent
{
// Some more auto properties
}
public class Event : BaseContent
{
// Some more auto properites
}
public class GenericResponse<T>
{
[XmlArray("Content")]
[XmlArrayItem("NewsObject", typeof(News)]
[XmlArrayItem("EventObject", typeof(Event)]
public List<T> ContentItems { get; set; }
}
public class NewsResponse : GenericResponse<News> {}
public class EventResponse : GenericResponse<Event> {}
Events.cs
public class BaseContent
{
// Some auto properties
}
public class News : BaseContent
{
// Some more auto properties
}
public class Event : BaseContent
{
// Some more auto properites
}
public class GenericResponse<T>
{
[XmlArray("Content")]
[XmlArrayItem("NewsObject", typeof(News)]
[XmlArrayItem("EventObject", typeof(Event)]
public List<T> ContentItems { get; set; }
}
public class NewsResponse : GenericResponse<News> {}
public class EventResponse : GenericResponse<Event> {}
GenericResponse.cs
public class BaseContent
{
// Some auto properties
}
public class News : BaseContent
{
// Some more auto properties
}
public class Event : BaseContent
{
// Some more auto properites
}
public class GenericResponse<T>
{
[XmlArray("Content")]
[XmlArrayItem("NewsObject", typeof(News)]
[XmlArrayItem("EventObject", typeof(Event)]
public List<T> ContentItems { get; set; }
}
public class NewsResponse : GenericResponse<News> {}
public class EventResponse : GenericResponse<Event> {}
公共类GenericResponse
{
[XmlArray(“内容”)]
[XmlArrayItem(“新闻对象”,类型(新闻)]
[XmlArrayItem(“事件对象”,类型(事件)]
公共列表ContentItems{get;set;}
}
NewsResponse.cs
public class BaseContent
{
// Some auto properties
}
public class News : BaseContent
{
// Some more auto properties
}
public class Event : BaseContent
{
// Some more auto properites
}
public class GenericResponse<T>
{
[XmlArray("Content")]
[XmlArrayItem("NewsObject", typeof(News)]
[XmlArrayItem("EventObject", typeof(Event)]
public List<T> ContentItems { get; set; }
}
public class NewsResponse : GenericResponse<News> {}
public class EventResponse : GenericResponse<Event> {}
公共类新闻响应:GenericResponse{}
EventResponse.cs
public class BaseContent
{
// Some auto properties
}
public class News : BaseContent
{
// Some more auto properties
}
public class Event : BaseContent
{
// Some more auto properites
}
public class GenericResponse<T>
{
[XmlArray("Content")]
[XmlArrayItem("NewsObject", typeof(News)]
[XmlArrayItem("EventObject", typeof(Event)]
public List<T> ContentItems { get; set; }
}
public class NewsResponse : GenericResponse<News> {}
public class EventResponse : GenericResponse<Event> {}
公共类事件响应:GenericResponse{}
如您所见,我有一个基类BaseContent和从中派生的两个类。接下来我有一个通用响应类,因为xml文件的结构总是相同的,但它们在某些属性上有所不同
我想我可以用[XmlArrayItem]
指定特定类使用的名称。但现在我得到了错误:
System.InvalidOperationException:无法生成临时类(结果=1)。
错误CS0012:在未引用的程序集中定义了类型“System.Object”。必须添加对程序集“System.Runtime,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用
我无法添加此引用,因为我正在使用Windows 8应用程序
如果我将其中一个[XmlArrayItem]
注释掉,它运行良好
有人有办法解决这个问题吗
更新
我可以不使用DataContractSerializer,因为我必须使用XmlAttributes,考虑使用DataContractSerializer而不是XmlSerializer XmlSerializer正在运行时生成一个临时程序集,以加快速度 序列化-以后的反序列化过程。因此需要编译代码 另一方面,DataContractSerializer没有。如果您使用DataContractSerializer 方法您必须使用适当的属性,以便控制“继承性”
问题。我找到了我的解决方案。不是最好的,但它正在工作 我建议IXmlSerializable并自己处理这些东西:
public void ReadXml(System.Xml.XmlReader reader)
{
reader.Read();
reader.MoveToContent();
if (reader.LocalName == "AnotherNode")
{
var innerXml = Serializer<AnotherClass>.CreateSerializer();
Remove = (AnotherClass) innerXml.Deserialize(reader);
reader.MoveToContent();
}
reader.Read();
// Here is the trick
if (reader.IsStartElement())
{
do
{
var innerXml = Serializer<T>.CreateSerializer();
var obj = (T) innerXml.Deserialize(reader);
Updates.Add(obj);
} while (reader.MoveToContent() == XmlNodeType.Element);
}
}
public void WriteXml(System.Xml.XmlWriter writer)
{
var removeSerializer = Serializer<RemoveElement>.CreateSerializer();
removeSerializer.Serialize(writer, Remove);
if (Updates.Any())
{
var innerXml = Serializer<T>.CreateSerializer();
writer.WriteStartElement("ContentUpdates");
foreach (var update in Updates)
{
innerXml.Serialize(writer, update);
}
writer.WriteEndElement();
}
}
public void ReadXml(System.Xml.XmlReader)
{
reader.Read();
reader.MoveToContent();
if(reader.LocalName==“另一个节点”)
{
var innerXml=Serializer.CreateSerializer();
Remove=(另一个类)innerXml.Deserialize(读取器);
reader.MoveToContent();
}
reader.Read();
//这里是诀窍
if(reader.IsStartElement())
{
做
{
var innerXml=Serializer.CreateSerializer();
var obj=(T)innerXml.Deserialize(reader);
更新。添加(obj);
}while(reader.MoveToContent()==XmlNodeType.Element);
}
}
public void WriteXml(System.Xml.XmlWriter)
{
var removeSerializer=Serializer.CreateSerializer();
序列化(writer,Remove);
if(Updates.Any())
{
var innerXml=Serializer.CreateSerializer();
writer.writeStarteElement(“内容更新”);
foreach(更新中的var更新)
{
序列化(编写器,更新);
}
writer.writeedelement();
}
}
编辑:请随意下载
您没有提供对象的所有属性,因此请允许我添加一些-仅作为示例:
public class BaseContent
{
[XmlAttribute("Name")]
public string Name { get; set; }
}
[XmlType(TypeName = "EventObject")]
public class Event : BaseContent
{
[XmlAttribute("EventId")]
public int EventId { get; set; }
}
[XmlType(TypeName = "NewsObject")]
public class News : BaseContent
{
[XmlAttribute("NewsId")]
public int NewsId { get; set; }
}
GenericResponse.cs可以这样定义-无需为数组项指定typeof:
public class GenericResponse<T>
{
[XmlArray("Content")]
public List<T> ContentItems { get; set; }
public GenericResponse()
{
this.ContentItems = new List<T>();
}
}
公共类GenericResponse
{
[XmlArray(“内容”)]
公共列表ContentItems{get;set;}
公共一般责任()
{
this.ContentItems=新列表();
}
}
然后是响应类:
public class EventResponse : GenericResponse<Event>
{
}
public class NewsResponse : GenericResponse<News>
{
}
公共类事件响应:GenericResponse
{
}
公共类新闻响应:GenericResponse
{
}
示例1:序列化EventResponse对象
var response=neweventresponse
{
ContentItems=新列表
{
新事件{
EventId=1,
Name=“事件1”
},
新事件{
EventId=2,
Name=“事件2”
}
}
};
字符串xml=XmlSerializer.Serialize(响应);
输出XML:
<?xml version="1.0" encoding="utf-8"?>
<EventResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Content>
<EventObject Name="Event 1" EventId="1" />
<EventObject Name="Event 2" EventId="2" />
</Content>
</EventResponse>
如果你在NewsResponse中尝试同样的方法,它会很好的工作。顺便说一句,我正在使用我的,点击链接了解更多信息
XmlSerializer.cs:
/// <summary>
/// XML serializer helper class. Serializes and deserializes objects from/to XML
/// </summary>
/// <typeparam name="T">The type of the object to serialize/deserialize.
/// Must have a parameterless constructor and implement <see cref="Serializable"/></typeparam>
public class XmlSerializer<T> where T: class, new()
{
/// <summary>
/// Deserializes a XML string into an object
/// Default encoding: <c>UTF8</c>
/// </summary>
/// <param name="xml">The XML string to deserialize</param>
/// <returns>An object of type <c>T</c></returns>
public static T Deserialize(string xml)
{
return Deserialize(xml, Encoding.UTF8, null);
}
/// <summary>
/// Deserializes a XML string into an object
/// Default encoding: <c>UTF8</c>
/// </summary>
/// <param name="xml">The XML string to deserialize</param>
/// <param name="encoding">The encoding</param>
/// <returns>An object of type <c>T</c></returns>
public static T Deserialize(string xml, Encoding encoding)
{
return Deserialize(xml, encoding, null);
}
/// <summary>
/// Deserializes a XML string into an object
/// </summary>
/// <param name="xml">The XML string to deserialize</param>
/// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlReaderSettings"/></param>
/// <returns>An object of type <c>T</c></returns>
public static T Deserialize(string xml, XmlReaderSettings settings)
{
return Deserialize(xml, Encoding.UTF8, settings);
}
/// <summary>
/// Deserializes a XML string into an object
/// </summary>
/// <param name="xml">The XML string to deserialize</param>
/// <param name="encoding">The encoding</param>
/// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlReaderSettings"/></param>
/// <returns>An object of type <c>T</c></returns>
public static T Deserialize(string xml, Encoding encoding, XmlReaderSettings settings)
{
if (string.IsNullOrEmpty(xml))
throw new ArgumentException("XML cannot be null or empty", "xml");
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
using (MemoryStream memoryStream = new MemoryStream(encoding.GetBytes(xml)))
{
using (XmlReader xmlReader = XmlReader.Create(memoryStream, settings))
{
return (T) xmlSerializer.Deserialize(xmlReader);
}
}
}
/// <summary>
/// Deserializes a XML file.
/// </summary>
/// <param name="filename">The filename of the XML file to deserialize</param>
/// <returns>An object of type <c>T</c></returns>
public static T DeserializeFromFile(string filename)
{
return DeserializeFromFile(filename, new XmlReaderSettings());
}
/// <summary>
/// Deserializes a XML file.
/// </summary>
/// <param name="filename">The filename of the XML file to deserialize</param>
/// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlReaderSettings"/></param>
/// <returns>An object of type <c>T</c></returns>
public static T DeserializeFromFile(string filename, XmlReaderSettings settings)
{
if (string.IsNullOrEmpty(filename))
throw new ArgumentException("filename", "XML filename cannot be null or empty");
if (! File.Exists(filename))
throw new FileNotFoundException("Cannot find XML file to deserialize", filename);
// Create the stream writer with the specified encoding
using (XmlReader reader = XmlReader.Create(filename, settings))
{
System.Xml.Serialization.XmlSerializer xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(T));
return (T) xmlSerializer.Deserialize(reader);
}
}
/// <summary>
/// Serialize an object
/// </summary>
/// <param name="source">The object to serialize</param>
/// <returns>A XML string that represents the object to be serialized</returns>
public static string Serialize(T source)
{
// indented XML by default
return Serialize(source, null, GetIndentedSettings());
}
/// <summary>
/// Serialize an object
/// </summary>
/// <param name="source">The object to serialize</param>
/// <param name="namespaces">Namespaces to include in serialization</param>
/// <returns>A XML string that represents the object to be serialized</returns>
public static string Serialize(T source, XmlSerializerNamespaces namespaces)
{
// indented XML by default
return Serialize(source, namespaces, GetIndentedSettings());
}
/// <summary>
/// Serialize an object
/// </summary>
/// <param name="source">The object to serialize</param>
/// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlWriterSettings"/></param>
/// <returns>A XML string that represents the object to be serialized</returns>
public static string Serialize(T source, XmlWriterSettings settings)
{
return Serialize(source, null, settings);
}
/// <summary>
/// Serialize an object
/// </summary>
/// <param name="source">The object to serialize</param>
/// <param name="namespaces">Namespaces to include in serialization</param>
/// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlWriterSettings"/></param>
/// <returns>A XML string that represents the object to be serialized</returns>
public static string Serialize(T source, XmlSerializerNamespaces namespaces, XmlWriterSettings settings)
{
if (source == null)
throw new ArgumentNullException("source", "Object to serialize cannot be null");
string xml = null;
XmlSerializer serializer = new XmlSerializer(source.GetType());
using (MemoryStream memoryStream = new MemoryStream())
{
using (XmlWriter xmlWriter = XmlWriter.Create(memoryStream, settings))
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));
x.Serialize(xmlWriter, source, namespaces);
memoryStream.Position = 0; // rewind the stream before reading back.
using (StreamReader sr = new StreamReader(memoryStream))
{
xml = sr.ReadToEnd();
}
}
}
return xml;
}
/// <summary>
/// Serialize an object to a XML file
/// </summary>
/// <param name="source">The object to serialize</param>
/// <param name="filename">The file to generate</param>
public static void SerializeToFile(T source, string filename)
{
// indented XML by default
SerializeToFile(source, filename, null, GetIndentedSettings());
}
/// <summary>
/// Serialize an object to a XML file
/// </summary>
/// <param name="source">The object to serialize</param>
/// <param name="filename">The file to generate</param>
/// <param name="namespaces">Namespaces to include in serialization</param>
public static void SerializeToFile(T source, string filename, XmlSerializerNamespaces namespaces)
{
// indented XML by default
SerializeToFile(source, filename, namespaces, GetIndentedSettings());
}
/// <summary>
/// Serialize an object to a XML file
/// </summary>
/// <param name="source">The object to serialize</param>
/// <param name="filename">The file to generate</param>
/// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlWriterSettings"/></param>
public static void SerializeToFile(T source, string filename, XmlWriterSettings settings)
{
SerializeToFile(source, filename, null, settings);
}
/// <summary>
/// Serialize an object to a XML file
/// </summary>
/// <param name="source">The object to serialize</param>
/// <param name="filename">The file to generate</param>
/// <param name="namespaces">Namespaces to include in serialization</param>
/// <param name="settings">XML serialization settings. <see cref="System.Xml.XmlWriterSettings"/></param>
public static void SerializeToFile(T source, string filename, XmlSerializerNamespaces namespaces, XmlWriterSettings settings)
{
if (source == null)
throw new ArgumentNullException("source", "Object to serialize cannot be null");
XmlSerializer serializer = new XmlSerializer(source.GetType());
using (XmlWriter xmlWriter = XmlWriter.Create(filename, settings))
{
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(T));
x.Serialize(xmlWriter, source, namespaces);
}
}
#region Private methods
private static XmlWriterSettings GetIndentedSettings()
{
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
xmlWriterSettings.IndentChars = "\t";
return xmlWriterSettings;
}
#endregion
}
//
///XML序列化程序帮助程序类。将对象从XML序列化并反序列化为XML
///
///要序列化/反序列化的对象的类型。
///必须具有无参数构造函数和实现
公共类XmlSerializer,其中T:class,new()
{
///
///将XML字符串反序列化为对象
///默认编码:UTF8
///
///要反序列化的XML字符串
///T型物体
公共静态T反序列化(字符串xml)
{
返回反序列化(xml,Encoding.UTF8,null);
}
///
///将XML字符串反序列化为对象
///默认编码:UTF8
///
///要反序列化的XML字符串
///编码
///T型物体
公共静态T反序列化(字符串xml,编码)
{
返回反序列化(xml、编码、null);
}
///
///将XML字符串反序列化为对象
///
///要反序列化的XML字符串
///XML序列化设置。
///T型物体
公共静态T反序列化(字符串xml、XmlReaderSettings设置)
{
返回反序列化(xml,Encoding.UTF8,settings);
}
///
///将XML字符串反序列化为对象
///
///要反序列化的XML字符串
///编码
///XML序列化设置。
///T型物体
公共静态T反序列化(字符串xml、编码、XmlReaderSettings设置)
{
if(string.IsNullOrEmpty(xml))
抛出新的Ar