c# Deserializing an element based on it's parent node's name
Posted
by
daveharnett
on Stack Overflow
See other posts from Stack Overflow
or by daveharnett
Published on 2012-03-30T17:08:32Z
Indexed on
2012/03/30
17:29 UTC
Read the original article
Hit count: 407
The XML I'm working with has the following structure:
<fixture_statistics>
<home_player_1 id="2306143" teamid="2">
<element_1>Some Data</element_1>
<element_2>Some Data</element_2>
</home_player_1>
<home_player_2 id="2306144" teamid="2">
<element_1>Some Data</element_1>
<element_2>Some Data</element_2>
</home_player_2>
</fixture_statistics>
Now the code to deserialize it would normally look like this:
[XmlRootAttribute("fixture_statistics", Namespace = "", IsNullable = false)]
public class FixtureRoot
{
[XmlElement("home_player_1")]
[XmlElement("home_player_2")]
public List<FixtureStats> fixtures { get; set; }
}
public class FixtureStats
{
public string element_1;
[XMLElement("element_2")]
public string elementTwo;
}
Here's the question: I'd like the FixtureStats class to have a 'position' property which corrosponds to it's parent's element name (so the FixtureStat object corrosponding to home_player_1 would have position=1). Can this be done with the built-in serialization atrributes?
If it's not possible, what's the cleanest workaround? Bear in mind that each document will have about 50 player elements, each with about 50 'child' data elements.
© Stack Overflow or respective owner