xmlからオブジェクトを逆シリアル化していますが、KeywordListとResponseListの文字列は常に空になります。Xml欠落している文字列を逆シリアル化する
多くのことを試してみると、それは正しいとは言えません。私がそれを読者でループすると、値が表示されます。ここで
using System;
using System.Collections.Specialized;
using System.Xml;
using System.Xml.Serialization;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
XmlSerializer serializer = new XmlSerializer(typeof(ElizaResponses));
using (XmlReader reader = XmlReader.Create("eliza.xml"))
{
ElizaResponses responses = (ElizaResponses)serializer.Deserialize(reader);
reader.Close();
// This reads in all data
//StreamReader reader = new StreamReader("eliza.xml");
//reader.ReadToEnd();
//reader.MoveToContent();
//while (reader.Read())
//{
// if(reader.NodeType == XmlNodeType.Text)
// {
// Console.WriteLine("{0}, {1}", reader.NodeType, reader.Value);
// }
// else
// {
// Console.WriteLine("{0}, {1}", reader.NodeType, reader.Name);
// }
//}
}
Console.ReadLine();
}
}
[Serializable]
[XmlRoot("ElizaResponses")]
[XmlType("ElizaResponses")]
public sealed class ElizaResponses
{
ElizaConversation[] chatList;
public ElizaConversation[] ChatList
{
get { return this.chatList; }
set { this.chatList = value; }
}
}
[Serializable]
public sealed class ElizaConversation
{
// Tried List<string>, string[], StringCollection
StringCollection keywordList = new StringCollection();
StringCollection responseList = new StringCollection();
// Tried [XmlArray], [XmlElement]
[XmlElement("KeywordList")]
[XmlArrayItem("string", typeof(string))]
StringCollection KeywordList
{
get { return this.keywordList; }
set { this.keywordList = value; }
}
[XmlArray("ResponseList")]
[XmlArrayItem("string", typeof(string))]
StringCollection ResponseList
{
get { return this.responseList; }
set { this.responseList = value; }
}
}
}
は、私は例外を取得していないし、答えを見つけapp.configファイル
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.diagnostics>
<switches>
<add name="XmlSerialization.Compilation" value="4" />
</switches>
</system.diagnostics>
</configuration>