locked
Read XML with XDocument RRS feed

  • Question

  • hello

            I have following scenario

     Groups contain Nth Group  (<Groups>  <Group></Group>  .... Nth </Groups>)
    each Group Contain Nth Nodes  (<Group>  <Nodes></Nodes>  .... Nth </Group>)
    each Nodes contail Nth node  
    (<Nodes>  <Node></Node>  .... Nth </Nodes>)
    Each Node contain information in elements.

        <Groups id="ACTIVE">

          <Group id="1">

               <Nodes>

                    <Node id="1">

                      <CompontantID>TE.TE-REGCO.01.01.</CompontantID>

                      <IP>172.16.32.1</IP>

                      <Port>6000</Port>

                      <ImageIndex>ImageName</ImageIndex>

                      <ToolTip>Connected</ToolTip>

            </Node>

                    <Node id="2">

                      <CompontantID>TE.TE-REGCO.01.01.</CompontantID>

                      <IP>172.16.32.1</IP>

                      <Port>6000</Port>

                      <ImageIndex>ImageName</ImageIndex>

                      <ToolTip>Connected</ToolTip>

            </Node>

            </Nodes>

          </Group>  

     

          <Group id="2">

              <Nodes>

                   <Node id="1">

                      <CompontantID>TE.TE-REGCO.01.01.</CompontantID>

                      <IP>172.16.32.1</IP>

                      <Port>6000</Port>

                      <ImageIndex>ImageName</ImageIndex>

                      <ToolTip>Connected</ToolTip>

            </Node>

                   <Node id="2">

                      <CompontantID>TE.TE-REGCO.01.01.</CompontantID>

                      <IP>172.16.32.1</IP>

                      <Port>6000</Port>

                      <ImageIndex>ImageName</ImageIndex>

                      <ToolTip>Connected</ToolTip>

            </Node>

            </Nodes>

          </Group>

     

        </Groups>

     

     

    Now i want to read each Group  using XDOCUEMENT 

     

     

     

     

     

     


    It's Me

    • Edited by It_s Meee Wednesday, May 18, 2011 1:43 PM
    Wednesday, May 18, 2011 1:24 PM

Answers

  • XDocument xdoc = new XDocument();
    xdoc.Load(path_to_file or another overload);
    
    var groupElements = from el in xdoc.Descendants().Elements("Group") select el;
    
    //groupElements is now a collection of XElements
    
    foreach(XElement xe in groupElements)
    {
      //do something
    }
    
    Is this helpful? 
    

    Bob - www.crowcoder.com
    • Marked as answer by It_s Meee Thursday, May 19, 2011 5:30 AM
    • Unmarked as answer by It_s Meee Thursday, May 19, 2011 5:56 AM
    • Marked as answer by It_s Meee Thursday, May 19, 2011 6:56 AM
    Wednesday, May 18, 2011 2:17 PM

All replies

  • what does this have to do with c#?
    Wednesday, May 18, 2011 1:29 PM
  • hello

                well Actually I want to load each group element using Xdocument. (i Forget to write)

    regards


    It's Me
    Wednesday, May 18, 2011 1:33 PM
  • XDocument xdoc = new XDocument();
    xdoc.Load(path_to_file or another overload);
    
    var groupElements = from el in xdoc.Descendants().Elements("Group") select el;
    
    //groupElements is now a collection of XElements
    
    foreach(XElement xe in groupElements)
    {
      //do something
    }
    
    Is this helpful? 
    

    Bob - www.crowcoder.com
    • Marked as answer by It_s Meee Thursday, May 19, 2011 5:30 AM
    • Unmarked as answer by It_s Meee Thursday, May 19, 2011 5:56 AM
    • Marked as answer by It_s Meee Thursday, May 19, 2011 6:56 AM
    Wednesday, May 18, 2011 2:17 PM
  • Hi

    There are the groups

    using System;
    using System.Linq;
    using System.Xml.Linq;
    using System.IO;
    using System.Collections.Generic;

    class Program
    {
        static void Main(string [] args)
        {
            XDocument doc = XDocument.Load("Content.xml");
            XElement root = doc.Root;

            IEnumerable<XElement> groups = root.Elements(XName.Get("Group"));
           
            Console.Read();
        }
    }

    the IEnumerable hold all the group elements


    The complexity resides in the simplicity Follow me at: http://smartssolutions.blogspot.com
    Wednesday, May 18, 2011 5:33 PM