• Issue with XML in C#
    1 replies, posted
I'm trying to fetch some attribute values from some child elements using the parent element's attribute but I'm having some isssues doing this. List keeps returning "System.Linq.Enumerable+WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]" instead of the actual value I'm trying to fetch. Am I parsing it wrong? Is there something I need to do? I'm stuck. /*code*/         static void Main(string[] args)         {             string jobno = "";             jobno = Console.ReadLine();             XDocument Xdco = XDocument.Load("C:/Users/user2/Documents/jobbook_db.xml");             var value1 = from i in Xdco.Root.Descendants("job")                        where i.Attribute("value").Value == jobno.ToString()                        select i.Element("company").Attribute("value").Value;             Console.WriteLine(value1);             Console.ReadLine();         } /* ------XML from file (not included in code above, obviously----- <?xml version="1.0" encoding="ISO-8859-1"?> <jobs> <job value="800"> <company value="Microsoft"/> <orderno value="22"/> <noof value="3"/> <desc value="Something in here"/> <finish value="Black"/> <status value="Done"/> <dateord value="19/10/1997"/> <deldate value="18/9/1998"/> <delnote value="Something here"/> <delprice value="44"/> <invoiced value="98"/> <wip value="fhjgkdhgjkfd"/> <dateinv value="gfgfgfgfgf"/> </job> </jobs>*/
Your query is correct, in that you are filtering by the job number and then projecting the company name. But what you are actually setting value1 to is the resulting IQueryable, you still need to collapse the resulting query with a function like ToArray/First/Single etc. I'd be wary of using var's in these situations as you can run into complications when you're not entirely sure of the type of you getting returned. You can use FirstOrDefault if you don't think it's erroneous to have multiple job elements with the same ID. var value1 = (from i in Xdco.Root.Descendants("job") where i.Attribute("value").Value == jobno.ToString() select i.Element("company").Attribute("value").Value).SingleOrDefault();
Sorry, you need to Log In to post a reply to this thread.