如何使用带有层次结构类结构的 C# 反序列化
我正在尝试将一些 xml 文件反序列化为一些已简化为以下内容的类:
[XmlRoot("person")]
[Serializable]
public class Person
{
[XmlElement]
public Toy Toy { get; set; }
}
[Serializable]
public class ActionMan : Toy
{
[XmlElement("guns")]
public string Guns;
}
[Serializable]
public class Doll : Toy
{
[XmlElement("name")]
public String Name;
}
[XmlInclude(typeof(Doll))]
[XmlInclude(typeof(ActionMan))]
public class Toy
{
}
[TestFixture]
public class ToyTest
{
[Test]
public void testHierarchy()
{
String filePath = @"test\brother.xml";
String sisfilePath = @"test\sister.xml";
var serializer = new XmlSerializer(typeof(Person));
Person brother = (Person)serializer.Deserialize(new FileStream(filePath, FileMode.Open));
Person sister = (Person)serializer.Deserialize(new FileStream(sisfilePath, FileMode.Open));
Assert.IsNotNull(brother);
Assert.IsNotNull(sister);
Assert.IsAssignableFrom(typeof(ActionMan),brother.Toy);
Assert.IsAssignableFrom(typeof(Doll),sister.Toy);
}
}
我想使用 c# 序列化(我知道我可以使用我自己的反序列化器),并且我认为我可能只是缺少一个特定的标记我不知道(而且我确信我有多余的标签)。
这是 xml 文件之一:
<person>
<doll>
<name>Jill</name>
</doll>
</person>
我得到的错误是“预期:可从第三个断言上分配”
I'm trying to deserialize some xml files into some classes which have been simplified to the following:
[XmlRoot("person")]
[Serializable]
public class Person
{
[XmlElement]
public Toy Toy { get; set; }
}
[Serializable]
public class ActionMan : Toy
{
[XmlElement("guns")]
public string Guns;
}
[Serializable]
public class Doll : Toy
{
[XmlElement("name")]
public String Name;
}
[XmlInclude(typeof(Doll))]
[XmlInclude(typeof(ActionMan))]
public class Toy
{
}
[TestFixture]
public class ToyTest
{
[Test]
public void testHierarchy()
{
String filePath = @"test\brother.xml";
String sisfilePath = @"test\sister.xml";
var serializer = new XmlSerializer(typeof(Person));
Person brother = (Person)serializer.Deserialize(new FileStream(filePath, FileMode.Open));
Person sister = (Person)serializer.Deserialize(new FileStream(sisfilePath, FileMode.Open));
Assert.IsNotNull(brother);
Assert.IsNotNull(sister);
Assert.IsAssignableFrom(typeof(ActionMan),brother.Toy);
Assert.IsAssignableFrom(typeof(Doll),sister.Toy);
}
}
I want to use the c# Serialisation (I know I can use my own deserialiser) and I think I'm perhaps simply missing a particular tag that I don't know about (and I'm sure I've got superfluous tags).
here is one fo the xml files:
<person>
<doll>
<name>Jill</name>
</doll>
</person>
the error I get is "Expected: assignable from " on the third assert
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当我尝试序列化你的结构(人,以 ActionMan 作为玩具)时,我得到
我想这就是您处理类型继承的方式。
但我猜你无法更改已经序列化的 XML。
When I try to serialize your structure (person, who has ActionMan as Toy) i get
I guess this is how you can handle inheritance of your types.
But I guess you cant change your already serialised XML.
我所做的就是按照我想要的方式设计类结构,填充一些基本数据,然后将其序列化。然后检查它如何序列化并调整 Xml 属性。如果您的 Toy 类只有几个派生类,那么您可以使用 action-man 字段和 doll 字段(可能为空或不为空)分别对它们进行反序列化。
或者,为了从 Xml 文件转到 ac# 类,我使用
xsd.exe
工具使用xsd mydata.xml
生成.xsd
文件然后从该 ac# 类文件中使用xsd /c /l:cs mydata.xsd
。然后,我检查类数据,以获取有关如何定义类以及要使用哪些属性的线索。链接到 Microsoft 的 xsd 工具。
What I have done, is designed the class structure the way I want, fill in some basic data and then serialize it. Then examine how it serializes and adjust the Xml attributes. If your
Toy
class has only a few derivatives, then you can de-serialize them seperately with a action-man field and a doll field that might be null or not.Alternatively to go from an Xml file to a c# class I use the
xsd.exe
tool to generate a.xsd
file withxsd mydata.xml
and then from that a c# class file withxsd /c /l:cs mydata.xsd
. Then I examine the class data to get clues as to how to define my class and what attributes to use.link to xsd tool from Microsoft.
尝试以下操作
您可以从这里扩展。请记住,在属性名称中,大小写很重要。 序列化输出是
Brother.xmlister.xml
我得到的
输出是
Try the following
You can expand from here. Remember that, in the attribute names, casing matters. The serialization output I got is
brother.xml
sister.xml
The output is
类 person 应该包含“娃娃”属性而不是“玩具”属性,我的意思是名称。 XML 节点的名称必须与属性名称相同 - 大小写很重要。
Class person should contain a "doll" attribute instead of a "Toy" attribute, I mean the name. The XML node must have the same name as the attribute name -casing matters.