我想反序列化一個XML文件。但是,我只需要文件中的兩個元素。這裏是基本的標記:反序列化XML元素返回NULL
<Stuff>
<Details>
<Comment>I want whats in here.</Comment>
<LogLevel>And here too.</LogLevel>
</Details>
<Stuff>
反序列化我做了以下內容:
XmlSerializer deserializer;
FileStream stream = new FileStream(CONFIG_PATH, FileMode.Open);
XmlReader reader = new XmlTextReader(stream);
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "Stuff";
xRoot.IsNullable = true;
// Details configuration area.
Utilities.Details d = new Utilities.Details();
deserializer = new XmlSerializer((typeof(Details)), xRoot);
d = (Details)deserializer.Deserialize(reader);
System.Windows.MessageBox.Show(d.Comment);
最後保存的對象類:
/// <summary>
/// Configuration details.
/// </summary>
[Serializable()]
[XmlRoot(ElementName = "Details", IsNullable = true)]
public sealed class Details
{
public Details()
{
}
[XmlElement("Comment")]
public string Comment { get; set; }
[XmlElement("LogLevel")]
public string LogLevel { get; set; }
}
然而d.Comment和d無論我做什麼,LogLevel都會繼續返回null。有任何想法嗎?
真棒,工作就像一個魅力!謝謝馬克。 – bl4kh4k