是否有可能将内部xml元素反序列化为它的平等类?我有下面的XML片段:内部元素的XML反序列化
<?xml version="1.0" encoding="utf-8" ?>
<tileconfiguration xmlns="http://somenamespace/tile-configuration">
<tile top_left_x="3" top_left_y="1" bottom_right_x="38" bottom_right_y="48">
<child>
</child>
</tile>
</tileconfiguration>
和等价类表示<tile />
元素:
[System.Xml.Serialization.XmlRoot(ElementName = "tile")]
public class Tile : System.Xml.Serialization.IXmlSerializable
{
public System.Xml.Schema.XmlSchema GetSchema()
{
throw new NotImplementedException();
}
public void ReadXml(System.Xml.XmlReader reader)
{
throw new NotImplementedException();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
throw new NotImplementedException();
}
}
的问题是,每次我试图反序列化<tile_configuration />
内<tile />
实例 - XML解串器在文档(2,2)中抛出错误。
System.Xml.Serialization.XmlSerializer serial = new System.Xml.Serialization.XmlSerializer(typeof(Tile));
System.IO.TextReader t = new System.IO.StreamReader("c:\\temp\\deserial.xml");
Tile q = (Tile)serial.Deserialize(t);
如果我创建一个类来表示<tile_configuration />
并直接从deserialise,它工作正常,调试器上进入TileConfiguration
类ReadXml
方法从中我就可以管理孩子<tile />
的解析(和decendent )元素 - 但这需要每次重新读取整个xml文件。
简而言之,我是否需要读取和写入整个XML文件 - 每次我想要使用序列化/反序列化或从某种方式允许我忽略extranous外部元素并直接将相关的子xml元素反序列化到它们的根代码equivilents没有解析器夹紧错误?
非常感谢。
想这可能是这样的限制范围。太棒了,非常感谢我会放弃它。 – Yumbelie 2012-08-03 17:21:08
你有没有遇到过这种情况?我面临同样的问题 – Andy 2016-10-10 13:27:49