我尝试将xml序列化到我的类:
[XmlRoot(ElementName = "order")]
public class OrderModel
{
[XmlElement(ElementName = "purchaser")]
public CompanyModel Purchaser { get; set; }
[XmlElement(ElementName = "deliverver")]
public CompanyModel Deliverver { get; set; }
[XmlElement(ElementName = "position")]
public List<OrderPositionModel> Positions { get; set; }
}
public class OrderPositionModel
{
[XmlElement(ElementName = "id")]
public int id { get; set; }
[XmlElement(ElementName = "lp")]
public int OrdinalNumber { get; set; }
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "quantity")]
public int Quantity { get; set; }
}
序列化后,我得到具有良好List长度的对象,但其元素具有空id,lp,name和quantitiy。
更新 序列化方法
private object XmlReader(HttpPostedFileBase file, Type type)
{
var document = new XmlDocument();
document.Load(file.InputStream);
string stream = document.OuterXml;
StringReader strReader = null;
XmlSerializer serializer = null;
XmlTextReader xmlReader = null;
Object obj = null;
try
{
strReader = new StringReader(stream);
serializer = new XmlSerializer(type);
xmlReader = new XmlTextReader(strReader);
obj = serializer.Deserialize(xmlReader);
}
catch (Exception)
{
//Handle Exception Code
}
finally
{
if (xmlReader != null)
{
xmlReader.Close();
}
if (strReader != null)
{
strReader.Close();
}
}
return obj;
}
像这样,然后我添加使用块
更新#2 我试试这个xml
<order>
<purchaser>
<name>name1</name>
<address>address1</address>
</purchaser>
<deliverver>
<name>name2</name>
<address>address2</address>
</deliverver>
<position id="17" name="pos1" lp="1" quantity="1"/>
<position id="21" name="pos2" lp="2" quantity="2"/>
</order>
答案 0 :(得分:1)
最简单的工作示例:
请注意,您的某些属性在使用XMLElement
时属于其属性,因此应使用XMLAttribute
进行修饰。
(我还删除了所有不必要的东西)
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApplication1
{
internal class Program
{
private static void Main(string[] args)
{
var filename = @"..\..\XMLFile1.xml";
var model = Read(filename);
}
private static OrderModel Read(string filename)
{
using (var reader = XmlReader.Create(filename))
{
var serializer = new XmlSerializer(typeof(OrderModel));
var model = (OrderModel) serializer.Deserialize(reader);
return model;
}
}
}
[XmlRoot(ElementName = "order")]
public class OrderModel
{
[XmlElement(ElementName = "purchaser")]
public CompanyModel Purchaser { get; set; }
[XmlElement(ElementName = "deliverver")]
public CompanyModel Deliverver { get; set; }
[XmlElement(ElementName = "position")]
public List<OrderPositionModel> Positions { get; set; }
}
public class OrderPositionModel
{
[XmlAttribute("id")]
public int id { get; set; }
[XmlAttribute("lp")]
public int OrdinalNumber { get; set; }
[XmlAttribute("name")]
public string Name { get; set; }
[XmlAttribute("quantity")]
public int Quantity { get; set; }
}
public class CompanyModel
{
[XmlElement(ElementName = "name")]
public string Name { get; set; }
[XmlElement(ElementName = "address")]
public string Address { get; set; }
}
}
答案 1 :(得分:0)
尝试以这种方式进行序列化,它对我有用
Example of using the class "ObjectSerialization"
try
{
parmExtRoute viagens = new parmExtRoute();
Stream streamXml = arquivoRecebido.InputStream;
var document = new XmlDocument();
document.Load(streamXml);
string stream = document.OuterXml;
viagens = ObjectSerialization.DeserializeObject<parmExtRoute>(stream);
retorno.Result = viagens;
}
catch (Exception ex)
{
retorno.ListErrors.Add(ex.Message);
}
return retorno;
public static class ObjectSerialization
{
public static string SerializeObject<T>(T dataObject)
{
if (dataObject == null)
return string.Empty;
try
{
using (var stringWriter = new StringWriter())
{
var namespaces = new XmlSerializerNamespaces();
var settings = new XmlWriterSettings();
settings.Encoding = new UnicodeEncoding(false, false);
settings.Indent = false;
settings.NewLineChars = string.Empty;
settings.NewLineHandling = NewLineHandling.None;
settings.OmitXmlDeclaration = true;
namespaces.Add(string.Empty, string.Empty);
var serializer = new XmlSerializer(typeof(T));
using (var xmlWriter = XmlWriter.Create(stringWriter, settings))
serializer.Serialize(xmlWriter, dataObject, namespaces);
return stringWriter.ToString();
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Erro ao serializar objeto. ", ex.Message) + DateTime.Now.ReturnDateLocale());
return string.Empty;
}
}
public static T DeserializeObject<T>(string xml) where T : class
{
if (string.IsNullOrEmpty(xml))
return Activator.CreateInstance<T>();
try
{
using (var stringReader = new StringReader(xml))
{
var serializer = new XmlSerializer(typeof(T), new Type[] { typeof(T) }); ;
return (T)serializer.Deserialize(stringReader);
}
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Erro ao deserializar objeto. ", ex.Message) + DateTime.Now.ReturnDateLocale());
return Activator.CreateInstance<T>();
}
}
public static string ObjectParamWs<T>(T dataObject)
{
string conteudo = SerializeObject<T>(dataObject);
string xml = conteudo;
return xml;
}
}