Reputation: 2861
I have the following XML I want to deserialize :
<Contents>
<Content>
<Title link="first">First text</Title>
<Text>This is the first thing you have to read</Text>
</Content>
<Content>
<Title link="second">Second text</Title>
<Text>This is the second thing you have to read</Text>
</Content>
<Content>
<Title link="third">Third text</Title>
<Text>This is the third thing you have to read</Text>
</Content>
</Contents>
And the associated classes :
[XmlRoot("Content")]
public class Help
{
[XmlElement("Title")]
public string Title { get; set; }
[XmlAttribute("link")]
public string Link { get; set; }
[XmlElement("Text")]
public string Text { get; set; }
}
[XmlRoot("Contents")]
public class Content
{
[XmlElement("Content")]
public List<Help> Contents { get; set; }
}
I'm fetching the data doing like this :
private Content ReturnHelpContent(string filename)
{
var fileLocation = @"C:\temp\"+filename+".xml";
Content texts = new Content();
XmlSerializer deserializer = new XmlSerializer(typeof(Content));
using (TextReader textReader = new StreamReader(fileLocation))
{
texts = (Content)deserializer.Deserialize(textReader);
}
return texts;
}
The fetching is almost good but my property Link is always null... After reading several threads here and there, I'm running out of ideas to make it work. Any help would be much appreciated.
Upvotes: 0
Views: 54
Reputation: 1660
Your XML format is not correct. Try changing it to this:
<Contents>
<Content link="first">
<Title >First text</Title>
<Text>This is the first thing you have to read</Text>
</Content>
<Content link="second">
<Title >Second text</Title>
<Text>This is the second thing you have to read</Text>
</Content>
<Content link="third">
<Title >Third text</Title>
<Text>This is the third thing you have to read</Text>
</Content>
Upvotes: 1