I have the following xml file
1
Computer
Infor
You can use LINQ to XML like
XDocument xDoc = XDocument.Load("test.XML");
var item = xDoc.Descendants("category")
.FirstOrDefault(r => r.Element("id").Value == "1");
if(item == null)
return;
string Name = item.Element("name").Value;
string Decription = item.Element("description").Value;
string active = item.Element("active").Value;
You can assign the results to your TextBoxes as per your requirement.
Load it first with XDocument
XDocument test = XDocument.Load("test.xml");
then,
var qry = (from item in test.Descendants("category")
where item.Element("id").Value == 1
select new
{
Name = (string)test.Element("name").Value
Description = (string)test.Element("description").Value
Active = (string)test.Element("active").Value
}).FirstOrDefault();
This created an anonymous type and you can now display your data like this:
if (qry != null) {
Console.WriteLine(qry.Name);
Console.WriteLine(qry.Description);
Console.WriteLine(qry.Active);
}
var xml = XDocument.Parse(xmlDataString);
var categoryElement = xml.Root
.Elements("category")
.FirstOrDefault(e => (string)e.Element("id") == "1");
Simply deserialize given XML in object and apply LINQ to Objects. MSDN
How about using Linq To Xml and converting the elements to a Dictionary<string,string>
?
var xDoc = XDocument.Parse(xml);
int id=1;
var dict = xDoc.XPathSelectElement("//category[id=" + id + "]")
.Elements()
.ToDictionary(e=>e.Name.LocalName , e=>(string)e);
Console.WriteLine(dict["description"]);