[英]Converting XML into a c# object
It is complicating me convert an XML to an object in C#. 这使我很难将XML转换为C#中的对象。
I want to convert an XML consisting of a list of objects 'Regla' with a series of fields (idRegla, DateFrom, DateTo, and a list of exceptions that may not appear). 我想转换一个XML,该XML包含对象“ Regla”的列表以及一系列字段(idRegla,DateFrom,DateTo和可能未出现的异常列表)。
I'm going crazy, I do not think it's that hard ... 我快疯了,我不认为这很难...
Here is the XML: 这是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>
Here is my class: 这是我的课:
[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; }
}
And this is my code: 这是我的代码:
[...]
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));
}
}
I don't understand what I'm doing wrong. 我不明白我在做什么错。 Is the ReglaRangoResult misconfigured class? ReglaRangoResult是否配置错误的类? What is missing? 什么东西少了? What is left? 剩下什么?
EXCEPTION RETURNED: 例外返回:
'Error reflecting type 'dllReglasNegocioMP.ReglaRangoResult' '反映类型为'dllReglasNegocioMP.ReglaRangoResult的错误'
In Visual Studio 2013 you can take the XML and select "Edit / Paste special / Paste XML as Classes". 在Visual Studio 2013中,您可以采用XML并选择“编辑/特殊粘贴/将XML作为类粘贴”。 When you have done that you can use use XmlSerializer to serialize and deserialize in an easy way. 完成后,可以使用XmlSerializer以一种简单的方式进行序列化和反序列化。
var serializer = new System.Xml.Serialization.XmlSerializer(typeof(MyPastedClass));
MyPastedClass obj;
using (var xmlStream = new StringReader(str))
{
obj = (MyPastedClass)serializer.Deserialize(xmlStream);
}
Take my class listed bellow. 以我班上列出的波纹管为例。 You can serilialize your object to real XML and compare it. 您可以将对象序列化为真实的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;
}
}
}
}
you get an exception that have an hierarchy that you do not define in your code. 您将获得一个异常,该异常的层次结构未在代码中定义。 if you'll set the right hierarchy it will work. 如果您设置正确的层次结构,它将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.