LINQ to XML : Understanding Annotations
Annotations are used for private use. It does not directly associated and known as Black Box in XML world. This can also be considered as meta tag.
Below is an example on how it can be handled. I have created a sample Windows Applications and will try to capture my understanding.
/// <summary>
/// Class for the Structure of the Data
/// </summary>
public class DataClass
{
public int ID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
}
public static XElement xmlData = new XElement("family");
private void Form2_Load(object sender, EventArgs e)
{
//Create a List in memory
List<DataClass> lstData = new List<DataClass>
{
new DataClass(){ID = 1, Name = "Debajyoti", Email = "d@contoso.com"},
new DataClass(){ID = 2, Name = "Sumitra", Email = "s@contoso.com"},
new DataClass(){ID = 3, Name = "Wriju", Email = "wg@contoso.com"},
new DataClass(){ID = 4, Name = "Tupur", Email = "t@contoso.com"},
new DataClass(){ID = 5, Name = "Writam", Email = "w@contoso.com"},
};
//Here create the XML which will hold the info also an annotation
foreach (DataClass data in lstData)
{
XElement member = new XElement("member",
new XAttribute("ID", data.ID),
new XAttribute("Name", data.Name));
//Adding Annotation to the element
member.AddAnnotation(data.Email);
xmlData.Add(member);
}
//Loading the List holding two values ID and Name
listBox1.DataSource = lstData;
listBox1.DisplayMember = "Name";
listBox1.ValueMember = "ID";
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex > 0)
{
//By ID we will capture the element to get the annotation
var email = xmlData.Descendants("member").
First(el => el.Attribute("ID").Value == listBox1.SelectedValue.ToString());
//Get the annotation to print that out.
label1.Text = email.Annotations<String>().First();
}
}
Namoskar!!!
Comments
- Anonymous
May 20, 2009
PingBack from http://asp-net-hosting.simplynetdev.com/linq-to-xml-understanding-annotations/