问题描述
我正在尝试使用DataContractSerializer读取xml文件并从xml实例化对象,但是我无法获得嵌入式类来正确地反序列化.我已经编写并测试了DatContract属性,xml和反序列化代码的多种组合,但均未成功.我读了很多文章,但是很少有关于使用嵌入式类反序列化的文章.
我过去曾经使用过XMLSerializer,没有任何问题.
下面是一个说明问题的简单示例.实例化了Parent类中的Child和GrandChild对象,但未使用XML文件中的值填充类中的属性.
在此先感谢您的帮助.解决此问题后,我肯定会忘记答案:)
I''m attempting to use DataContractSerializer to read an xml file and instantiate objects from the xml but I''m not able to get an embedded class to deserialize properly. I''ve written and tested numerous combinations of DatContract attributes, xml and deserializing code with no success. I''ve read so many articles but there are few that go into detail about deserializing with an embedded class.
I''ve used XMLSerializer in the past and had no problems.
Below is a simple example which illustrates the problem. The Child and GrandChild objects in the Parent class are instantiated but the properties in the classes are not populated with the values in the XML file.
Thanks in advance for any help. I''m sure I will forget the answer once this is resolved :)
//XML
<?xml version="1.0" encoding="utf-8"?>
<Parent>
<Child>
<Age>15</Age>
<EyeColor>blue</EyeColor>
</Child>
<FirstName>Joe</FirstName>
<Grandchild>
<Age>15</Age>
<EyeColor>blue</EyeColor>
</Grandchild>
<LastName>Smith</LastName>
</Parent>
//---------------------------------------------------------------
// Object classes
//---------------------------------------------------------------
[DataContract(Namespace = "")]
public class Parent
{
private Child _Child;
private Grandchild _Grandchild;
private string _FirstName;
private string _LastName;
[DataMember]
public Child Child
{
get
{
return _Child;
}
set
{
_Child = value;
}
}
[DataMember]
public Grandchild Grandchild
{
get
{
return _Grandchild;
}
set
{
_Grandchild = value;
}
}
[DataMember]
public string FirstName
{
get
{
return _FirstName;
}
set
{
_FirstName = value;
}
}
[DataMember]
public string LastName
{
get
{
return _LastName;
}
set
{
_LastName = value;
}
}
}
[DataContract]
public class Child
{
private int _Age;
private string _EyeColor;
[DataMember]
public int Age
{
get
{
return _Age;
}
set
{
_Age = value;
}
}
[DataMember]
public string EyeColor
{
get
{
return _EyeColor;
}
set
{
_EyeColor = value;
}
}
}
[DataContract]
public class Grandchild
{
private int _Age;
private string _EyeColor;
[DataMember]
public int Age
{
get
{
return _Age;
}
set
{
_Age = value;
}
}
[DataMember]
public string EyeColor
{
get
{
return _EyeColor;
}
set
{
_EyeColor = value;
}
}
}
DataContractSerializer ser = new DataContractSerializer(typeof(Parent));<br />
FileStream fs = new FileStream("D:/Sandbox/WebApplication1/xml/parent6.xml", FileMode.Open);<br />
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());<br />
<br />
//Deserialize the data and read it from the instance.<br />
Parent parent = (Parent)ser.ReadObject(reader, true);<br />
<br />
reader.Close();
//结果对象
父母(已实例化)
parent.Child(已实例化)
parent.Child.Age = 0
parent.Child.EyeColor = null
parent.FirstName =乔"
parent.GrandChild(已实例化)
parent.GrandChild.Age = 0
parent.GrandChild.EyeColor = null
parent.LastName ="Smith"
// Resulting Object
parent(instantiated)
parent.Child (instantiated)
parent.Child.Age = 0
parent.Child.EyeColor = null
parent.FirstName = "Joe"
parent.GrandChild (instantiated)
parent.GrandChild.Age = 0
parent.GrandChild.EyeColor = null
parent.LastName="Smith"
推荐答案
这篇关于无法使用DataContractSerializer反序列化嵌入式类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!