none
Linq + XML RRS feed

  • Question

  • I am admist creating a way to load multiple elements from my xml doc. The value I want to read is the <Code>2</Code> .. however, it appears that my code is only picking up the first element where I have about 10 of these child elements.

            <CodesToDump>
                    <Code>0</Code>
                   <Code>2</Code>

           </CodesToDump>

    var codes = from code in configXML.Descendants("CodesToDump")
                               select new
                               {                           
                                   Code = code.Element("Code")
                                   //Code = code.Elements("Code").Value, ... no idea...
                                  
                               };

    What is the best read multiple elements of 'Code'??

    Thursday, August 25, 2011 7:53 PM

Answers

  • Hi Eric;

    If you like using XPath you can. these two XPath queries in Linq will give the same results. Note you need to start with the root element as shown below.

    String xml = "<CodesToDump><Code>0</Code><Code>2</Code></CodesToDump>";
    
    XDocument configXML = XDocument.Parse(xml);
    XElement root = configXML.Root;
    
    var codes = from code in root.XPathSelectElements("child::Code")
          select code ;
    
    or this
    
    var codes = from code in root.XPathSelectElements("./Code")
          select code ; 
    

    See this link, LINQ to XML for XPath Users, for more details.

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, August 26, 2011 7:51 PM

All replies

  • Hi Eric;

    The following Linq to XML code will return all nodes with the tag Code. As XML nodes

     

    var codes = from code in configXML.Descendants("Code")
       select code ;
       
    
    foreach( var x in codes )
    {
     Console.WriteLine(x);
    }
    

    the codes variable codes will contain a IEnumerable<XElement> that when enumerated will look like the following:

     

    <Code>0</Code>
    <Code>2</Code>
     
    If you just want the values back then this query will do that.

     

    var codes = from code in configXML.Descendants("Code")
       select code.Value 
    

    Which will return an IEnumerable of String

     

    0
    2

    Or if the values will be all integer values and you want to return an IEnumerable of int's then this query will do that:

     

    var codes = from code in configXML.Descendants("Code")
       select (int) code;
    

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, August 26, 2011 4:08 AM
  • Thanks! And is there a way to use XPath in this linq statement since i have more than 1 section of <Code> tags in my xml? I find XPath doesn't work with the method "Descendants("//CodesToDump/Code")"

     

    EDIT: Okay, nevermind I should be good with

     

    configXML.Descendants("CodesToDump").Descendants("Code")

     

    instead of the XPath equivalent.

    Friday, August 26, 2011 6:51 PM
  • Hi Eric;

    If you like using XPath you can. these two XPath queries in Linq will give the same results. Note you need to start with the root element as shown below.

    String xml = "<CodesToDump><Code>0</Code><Code>2</Code></CodesToDump>";
    
    XDocument configXML = XDocument.Parse(xml);
    XElement root = configXML.Root;
    
    var codes = from code in root.XPathSelectElements("child::Code")
          select code ;
    
    or this
    
    var codes = from code in root.XPathSelectElements("./Code")
          select code ; 
    

    See this link, LINQ to XML for XPath Users, for more details.

     

     


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, August 26, 2011 7:51 PM