I would like to populate the DescriptionAttribute of a property, from a string that will be initialized from an xml file. The property will be used in a propertygrid.
The main thing is getting the descriptions from an xml file. How to get it into a const string that i can then use as the DescriptionAttribute of a property.
I have tried a few things with no success so any help would be appreciated.
or is there another wey to assign the xml values to the description? A typeconverter perhaps? Just point me to the right direction please.
public class1
{
string BaboonDescription = "";
string TigerDescription = "";
const string SnakeDescription = "A snake is bla bla bla";
// method that extracts the descriptions from the xml file.
public void PopulateFromXml(string xmlfile)
{
XDocument xDoc = XDocument.Load(xmlfile);
var items = from i in xDoc.Descendants("item")
select i;
foreach (var item in items)
{
switch (item.Attribute("name").Value)
{
case "Baboon":
BaboonDescription = item.Value; // Assigns BaboonDescription the description from xml.
break;
case "Tiger":
TigerDesscription = item.Value; // Assigns TigerDescription the description from xml.
break;
default:
break;
}
}
}
}
public class2 : class1
{
[Description(BaboonDescription)] // problem here. Telling me that I need const string. But i have to get the strings from an xml.
public string Baboon { get; set; }
[Description("tiger is bla bla")] // this one works but I want the description from the xml.
public string Tiger { get; set; }
[Description(SnakeDescription)] // this also works but I want the description from the xml.
public string Snake { get; set; }
}