I’m new at this so bear with me. Using C# and LINQ, I am trying to pull out one record from an XML file and I want to bind it’s Elements and Attributes to individual textblocks.
Lets say I have this xml file named Test.xml:
<Data>
<Test id=1>
<Text>"First Record"</Text>
</Test>
<Test id=2>
<Text>"Second Record"</Text>
</Test>
<Data>
I want to pass in a variable for the id so i can pull out that records information and bind it to individual textblocks
var r = 2 // just hardcoding for the example
XDocument d = XDocument.Load("Test.xml");
var q = from t in d.Descendants("Test")
where t.Attribute("id").Value == r
select new
{
id = t.Attribute("id").Value,
text = t.Element("text").Value
}
Now I would like to bind “id” and “text” to indivual Text blocks
idTextBlock.Text = id
textTextBlock.Text = text
This of course throws up all sorts of errors about id and text “not existing in this context”. All the examples I have seen have simply put the output lumped to a single string on the console which doesn’t help me. I am really new (2nd week) to programming so I may be doing this completely wrong. ANY assistance would be appreciated. Thanks!
I’ve corrected XML you’ve provided to get query below worked.
Corrected XML: (Closed
Datatag, id attribute value enclosed in double quotes)