I have been searching for an answer for 24 hours, but I did not find it. I am new to C#. I have found a tutorial, from where I have used this code:
XDocument loadedCustomData = XDocument.Load("PeopleCustom.xml");
var filteredData = from c in loadedCustomData.Descendants("Person")
where c.Attribute("Age").Value == current.ToString()
select new Person
{
FirstName = (string)c.Attribute("FirstName").Value,
};
listBox1.ItemsSource = filteredData;
public class Person
{
string firstname;
string lastname;
int age;
public string FirstName
{
get { return firstname; }
set { firstname = value; }
}
public string LastName
{
get { return lastname; }
set { lastname = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}
XML is like this:
<People>
<Person
FirstName="Kate"
LastName="Smith"
Age="1" />
<Person
...
/>
</People>
It works, but it puts all output into listbox. I want strings. I tried to get string from list box (like this listBox1.Items[0].ToString();), but all I get is something like this: ProjectName.MainPage+Person. I also tried to get it from filteredData, but no success.
Is there any way to get data from XML to strings? Thank you in advance for your answer
this code
makes a list of Person objects:
select New Person{.....}If you want a list of string of the
Person‘s first name, then all you have to do is change what object the LINQ is creating….now, it makes a new string from the
FirstNameattribute.after this linq is run, you will basically have a linq object that will produce a list of strings. if you want a List then modify like this:
if you want the first string in the list…