让我将XML转换为C#中的对象变得更加复杂。
我想转换一个由对象列表组成的XML' Regla'包含一系列字段(idRegla,DateFrom,DateTo和可能未显示的异常列表)。
我疯了,我认为这并不难......
这是XML:
<ListaReglas>
<REGLA>
<idRegla>2</idRegla>
<DateFrom>2013-12-01T00:00:00</DateFrom>
<DateTo>2015-07-25T00:00:00</DateTo>
<Excepciones>
<FECHA>2013-12-25T00:00:00</FECHA>
</Excepciones>
</REGLA>
<REGLA>
<idRegla>4</idRegla>
<DateFrom>2013-12-01T00:00:00</DateFrom>
<DateTo>2015-07-25T00:00:00</DateTo>
<Excepciones>
<FECHA>2013-12-25T00:00:00</FECHA>
</Excepciones>
</REGLA>
<REGLA>
<idRegla>5</idRegla>
<DateFrom>2013-12-01T00:00:00</DateFrom>
<DateTo>2015-07-25T00:00:00</DateTo>
<Excepciones>
<FECHA>2013-12-25T00:00:00</FECHA>
</Excepciones>
</REGLA>
<REGLA>
<idRegla>7</idRegla>
<DateFrom>2013-11-19T00:00:00</DateFrom>
<DateTo>2015-12-19T00:00:00</DateTo>
</REGLA>
</ListaReglas>
这是我的班级:
[Serializable]
[XmlTypeAttribute(AnonymousType = true)]
public class ReglaRangoResult
{
[XmlElement(ElementName = "idRegla", IsNullable = false)]
public int idRegla { get; set; }
[XmlElement(ElementName = "DateFrom", IsNullable = false)]
public DateTime DateFrom { get; set; }
[XmlElement(ElementName = "DateTo", IsNullable = false)]
public DateTime DateTo { get; set; }
[XmlElement(ElementName = "Excepciones", IsNullable = true)]
public List<DateTime> Excepciones { get; set; }
[XmlIgnore]
public int Peso { get; set; }
}
这是我的代码:
[...]
List<ReglaRangoResult> listaReglas = new List<ReglaRangoResult>();
XmlDoc xmlDoc = new XmlDoc(rdr.GetString(0));
foreach (XmlNode xmlNode in xmlDoc.SelectNodes("//ListaReglas/REGLA"))
{
listaReglas.Add(XmlToObject<ReglaRangoResult>(xmlNode.OuterXml));
}
[...]
public static T XmlToObject<T>(string xml)
{
using (var xmlStream = new StringReader(xml))
{
var serializer = new XmlSerializer(typeof(T));
return (T)serializer.Deserialize(XmlReader.Create(xmlStream));
}
}
我不明白我做错了什么。 ReglaRangoResult是否配置错误?缺什么?还剩下什么?
返回异常:
&#39;错误反映类型&#39; dllReglasNegocioMP.ReglaRangoResult&#39;
答案 0 :(得分:1)
在Visual Studio 2013中,您可以使用XML并选择“编辑/粘贴特殊/粘贴XML作为类”。完成后,您可以使用XmlSerializer以简单的方式序列化和反序列化。
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(MyPastedClass));
MyPastedClass obj;
using (var xmlStream = new StringReader(str))
{
obj = (MyPastedClass)serializer.Deserialize(xmlStream);
}
答案 1 :(得分:0)
参加下面列出的课程。您可以将对象序列化为真实XML并进行比较。
using System.Diagnostics.Contracts;
using System.Globalization;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace VmsSendUtil
{
/// <summary> Serializes and Deserializes any object to and from string </summary>
public static class StringSerializer
{
///<summary> Serializes object to string </summary>
///<param name="obj"> Object to serialize </param>
///<returns> Xml string with serialized object </returns>
public static string Serialize<T>(T obj)
{
Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>()));
var stringSerializer = new StringSerializer<T>();
return stringSerializer.Serialize(obj);
}
/// <summary> Deserializes object from string. </summary>
/// <param name="xml"> String with serialization XML data </param>
public static T Deserialize<T>(string xml)
{
Contract.Requires(!string.IsNullOrEmpty(xml));
Contract.Ensures(!Equals(Contract.Result<T>(), null));
var stringSerializer = new StringSerializer<T>();
return stringSerializer.Deserialize(xml);
}
}
/// <summary> Serializes and Deserializes any object to and from string </summary>
public class StringSerializer<T>
{
[ContractInvariantMethod]
private void ObjectInvariant()
{
Contract.Invariant(_serializer != null);
}
private readonly XmlSerializer _serializer = new XmlSerializer(typeof(T));
///<summary> Serializes object to string </summary>
///<param name="obj"> Object to serialize </param>
///<returns> Xml string with serialized object </returns>
public string Serialize(T obj)
{
Contract.Ensures(!string.IsNullOrEmpty(Contract.Result<string>()));
var sb = new StringBuilder();
using (var sw = new StringWriter(sb, CultureInfo.InvariantCulture))
{
var tw = new XmlTextWriter(sw) { Formatting = Formatting.Indented };
_serializer.Serialize(tw, obj);
}
string result = sb.ToString();
Contract.Assume(!string.IsNullOrEmpty(result));
return result;
}
/// <summary> Deserializes object from string. </summary>
/// <param name="xml"> String with serialization XML data </param>
public T Deserialize(string xml)
{
Contract.Requires(!string.IsNullOrEmpty(xml));
Contract.Ensures(!Equals(Contract.Result<T>(), null));
using (var stringReader = new StringReader(xml))
{
// Switch off CheckCharacters to deserialize special characters
var xmlReaderSettings = new XmlReaderSettings { CheckCharacters = false };
var xmlReader = XmlReader.Create(stringReader, xmlReaderSettings);
var result = (T)_serializer.Deserialize(xmlReader);
Contract.Assume(!Equals(result, null));
return result;
}
}
}
}
答案 2 :(得分:0)
您将获得一个异常,该异常具有您未在代码中定义的层次结构。 如果您设置了正确的层次结构,它将起作用。