Nikmaster
Nikmaster

Reputation: 17

Importing data from XML file to strings

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

Upvotes: 0

Views: 526

Answers (2)

scartag
scartag

Reputation: 17680

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

Upvotes: 0

Muad&#39;Dib
Muad&#39;Dib

Reputation: 29226

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();

Upvotes: 1

Related Questions