別のタグ (「データ」) 内の XML (「アイテム」) を、「アイテム」取得プロパティ内で別の逆シリアル化を使用せずに、より単純に逆シリアル化できますか。おそらく、「public Item[] Items」のある種の属性です。
XML:
<body>
<request></request>
<data><![CDATA[
<items>
<item>
<property1>Name1</property1>
<property2>111</property2>
</item>
<item>
<property1>Name2</property1>
<property2>222</property2>
</item>
<item>
<property1>Name3</property1>
<property2>333</property2>
</item>
</items>]]>
</data>
</body>
テストクラス:
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
const string xml = "<body>" +
"<request></request>" +
"<data><![CDATA[" +
"<items>" +
"<item>" +
"<property1>Name1</property1>" +
"<property2>111</property2>" +
"</item>" +
"<item>" +
"<property1>Name2</property1>" +
"<property2>222</property2>" +
"</item>" +
"<item>" +
"<property1>Name3</property1>" +
"<property2>333</property2>" +
"</item>" +
"</items>" +
"]]></data>" +
"</body>";
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);
var serializer = new XmlSerializer(typeof (Body));
var response = serializer.Deserialize(new XmlNodeReader(xmlDoc)) as Body;
Assert.IsNotNull(response);
Assert.AreEqual(3, response.Items.Length);
}
}
[Serializable, XmlRoot("body")]
public class Body
{
[XmlElement("request")]
public string Request;
[XmlElement("data")]
public string Data;
public Item[] Items
{
get
{
var document = new XmlDocument();
document.LoadXml(Data);
var serializer = new XmlSerializer(typeof(ItemList));
var response = serializer.Deserialize(new XmlNodeReader(document)) as ItemList;
return response.Items;
}
}
}
[Serializable, XmlRoot("items")]
public class ItemList
{
[XmlElement("item")]
public Item[] Items;
}
[Serializable]
public class Item
{
[XmlElement("property1")]
public string Property1;
[XmlElement("property2")]
public string Property2;
}