c# - Linq to XML: from query to variable -


i wrote this:

        xdocument doc = xdocument.load("test.xml");         string nodename = "mike";         var query = el in doc.descendants("dogs")                     (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 want load data, want put them variables, able put them later textboxes, radioboxes, etc. know how display messagebox.

not sure if refering this, can give try:

make new public class:

    public class xmlresut     {         public string name { get; set; }         public string breed { get; set; }         public string sex { get; set; } // maybe enum fit property better     } 

now change linq create new instances of defined class select new (http://www.dotnetperls.com/select-new)

    xdocument doc = xdocument.load("test.xml");     string nodename = "mike";     var query = el in doc.descendants("dogs")                 (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);     } 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -