0

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

2 Answers 2

1

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,
                           };

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....

select (string)c.Attribute("FirstName").Value );

now, it makes a new string from the FirstName attribute.

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:

XDocument loadedCustomData = XDocument.Load("PeopleCustom.xml");
var filteredData =( from c in loadedCustomData.Descendants("Person")
                           where c.Attribute("Age").Value == current.ToString()
                           select (string)c.Attribute("FirstName").Value ).ToList();

if you want the first string in the list...

filterdData.First();
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry, I cant vote up your answer, I do not have enough rep. Thanks again
Is there any way to convert filteredData directly to string, without using listbox? filteredData.ToString(); is not working
0

From the output you are getting listBox1.items contains person objects so you could try

var name = ((Person)listBox1.Items[0]).FirstName;

this should give you the value

1 Comment

Sorry, this did not work. It says that there is no definition for Firstname.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.