简体   繁体   中英

Linq to XML: from query to variable

I wrote this:

        XDocument doc = XDocument.Load("test.xml");
        string nodeName = "Mike";
        var query = from el in doc.Descendants("dogs")
                    where (string)el.Attribute("name") == nodeName
                    select
                    "Name: " + nodeName
                    + "\n" + "Breed: " + (string)el.Element("breed")
                    + "\n" + "Sex: " + (string)el.Element("sex");
        foreach (string data in query)
            MessageBox.Show(data);

Since I want to load this data, I want to put them into variables, to be able to put them later to textBoxes, radioBoxes, etc. For now I know only how to display it with MessageBox.

Not sure if you are refering to this, but you can give it a try:

Make a new public class:

    public class XmlResut
    {
        public string Name { get; set; }
        public string Breed { get; set; }
        public string Sex { get; set; } // Maybe a enum would fit this property better
    }

Now you change the LINQ to create new instances of the defined class with a select new ( http://www.dotnetperls.com/select-new )

    XDocument doc = XDocument.Load("test.xml");
    string nodeName = "Mike";
    var query = from el in doc.Descendants("dogs")
                where (string)el.Attribute("name") == nodeName
                select new XmlResult(){
                    Name = nodeName,
                    Breed = (string)el.Element("breed")
                    Sex = (string)el.Element("sex")
                };
    foreach (string data in query)
    {
        Console.WriteLine(data.Name);
        Console.WriteLine(data.Breed);
        Console.WriteLine(data.Sex);
    }

You can call Enumerable.ToList to store them in collection for later use.

var result = query.ToList();
foreach (string data in result)
        MessageBox.Show(data);

//result is available for later use

Okay, I don't know if there's better way to achieve what I wanted, but it works.

    public class Data
    {
    public string name { get; set; }
    public string breed { get; set; }
    public string sex { get; set; }
    }

And method:

                  XDocument doc = XDocument.Load(@"test.xml");
                  string nodeName = "Mike";
                  var data = from q in doc.Descendants("dogs")
                  where (string)q.Attribute("name") == nodeName
                      select new Data
                      {
                          name = q.Attribute("name").Value,
                          breed = q.Element("breed").Value,
                          sex = q.Element("sex").Value
                      };
                  foreach (var element in data)
                  {
                      textBox1.Text = element.name;
                      comboBox1.Text = element.sex;
                      textBox2.Text = element.breed;
                  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM