さて、私はタイプを持っています:
public class MonitorConfiguration
{
private string m_sourcePath;
private string m_targetPath;
public string TargetPath
{
get { return m_targetPath; }
set { m_targetPath = value; }
}
public string SourcePath
{
get { return m_sourcePath; }
set { m_sourcePath = value; }
}
//need a parameterless constructor, just for serialization
private MonitorConfiguration()
{
}
public MonitorConfiguration(string source, string target)
{
m_sourcePath = source;
m_targetPath = target;
}
}
これらのリストをシリアライズおよびデシリアライズすると、次のようになります
XmlSerializer xs = new XmlSerializer(typeof(List<MonitorConfiguration>));
using (Stream isfStreamOut = isf.OpenFile("Test1.xml", FileMode.Create))
{
xs.Serialize(isfStreamOut, monitoringPaths);
}
using (Stream isfStreamIn = isf.OpenFile("Test1.xml", FileMode.Open))
{
monitoringPaths = xs.Deserialize(isfStreamIn) as List<MonitorConfiguration>;
}
すべて正常に動作します。
ただし、属性のパブリック セッターを非表示にしたいです。これにより、XML シリアライザーによってシリアライズされなくなります。したがって、次のように独自に実装します。
クラス宣言を this: に変更し、public class MonitorConfiguration : IXmlSerializable
これらを追加します。
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
//make sure we read everything
while (reader.Read())
{
//find the first element we care about...
if (reader.Name == "SourcePath")
{
m_sourcePath = reader.ReadElementString("SourcePath");
m_targetPath = reader.ReadElementString("TargetPath");
// return;
}
}
}
public void WriteXml(System.Xml.XmlWriter writer)
{
writer.WriteElementString("SourcePath", m_sourcePath);
writer.WriteElementString("TargetPath", m_targetPath);
}
これはうまくいくようですが、リストから最初の項目しか取得できず、他の項目はすべて忘れられます。現在コメントアウトされているリターンの有無にかかわらず試しました。ここで何が間違っていますか?
これは、問題を説明する単なるスニペット コードであることに注意してください。私が使用している XML シリアライゼーション テクノロジは、永遠のメカニズムに限定されています。