内部の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 />
問題は、-内のインスタンスを逆シリアル化しようとするたびに、XMLデシリアライザーがドキュメント(2,2)でエラーを<tile_configuration />
スローすることです。
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 />
と、それは正常に機能し、デバッガーはクラスのReadXml
メソッドに入り、TileConfiguration
そこから子(および子孫)要素の解析を管理でき<tile />
ます-ただし、これには、の再読み取りが必要です。毎回xmlファイル全体。
一言で言えば; XMLファイル全体を読み書きする必要がありますか?シリアル化/逆シリアル化を使用するたびにルートxml要素から開始しますか、それとも無関係な外部要素を無視して、関連する子xml要素を直接逆シリアル化する方法がありますか?パーサーチャッキングエラーのない同等のコード?
とても感謝しております。