locked
Xml getting all parent node RRS feed

  • Question

  • Hi,

    I need any help at all. Any ideas, codes, samples would be greatly appreciated. This is i think a simple problem, but i cant seem to get it to work. I have a XML file containing:

    <myForm type="controls">
        <Table1 code="text">
              <Table2 code="text">
                  <Table3 code="text">
                     <Filename name="1" />
                     <Filename name="2" />
                     <Filename name="3" />
                  </Table3>
              </Table2>
        </Table1>
    </myForm>

    Using System.xml, i want to get the path or the parents of the Filename with the name "3". Im expecting that the result will be "Table3", "Table2", "Table1" &  "myForm". How can i do this? Thank you so much in advance.

    Thanks and Regards,


    Tuesday, July 1, 2008 11:11 AM

Answers

  • You can examine the ParentNode property of the XmlElement that represents <Filename name="3"/> and the ParentNode property of that and continue up until you get to the Document node (NodeType==Document).

    XmlElement e; // initiate it to the node you want the path of.
    string s="";
    while(XmlElement.Parent!=null){
      e=XmlElement.Parent;
      if (e.NodeType==NodeType.Document){
        break;
      }
      s=e.Name+"\\"+s;
    }

    Something like that. Note - Untested code.


    /Ruben RJJournal
    • Marked as answer by Rain3204 Wednesday, July 2, 2008 6:48 AM
    • Marked as answer by Rain3204 Wednesday, July 2, 2008 6:48 AM
    • Unmarked as answer by Rain3204 Wednesday, July 2, 2008 6:48 AM
    • Unmarked as answer by Rain3204 Wednesday, July 2, 2008 6:48 AM
    • Marked as answer by Rain3204 Wednesday, July 2, 2008 6:48 AM
    Tuesday, July 1, 2008 1:03 PM
  • I tried to solve ur query :
    There is button for which i have written an event handler
    I it i load xml document and call a user defined function GetParentNodes(...) :
    private void button1_Click(object sender, EventArgs e) 
            { 
                XmlDocument doc = new XmlDocument(); 
                doc.Load("test.xml"); 
                XmlElement root = doc.DocumentElement; 
                XmlNodeList nodeList = root.GetElementsByTagName("Filename"); 
                //string s; 
                XmlNode node; 
                for (int i = 0; i < nodeList.Count; i++) 
                { 
                    node = nodeList[i]; 
                    if (node.Attributes["name"].Value == "3") 
                    { 
                        foreach (string s in GetParentNodes(node)) 
                            Console.WriteLine(s); 
                        break; 
                    } 
                } 
                //Console.Write(doc.DocumentElement.Name); 
            } 
     
            private IEnumerable<string> GetParents(XmlNode node) 
            { 
                for (; ; ) 
                { 
                    nodenode = node.ParentNode; 
                    if (node == null || node.NodeType == XmlNodeType.Document) break; 
                    yield return node.Name; 
                } 
            } 


    Wife is like a Software, Lots of bugs.
    • Proposed as answer by Rain3204 Wednesday, July 2, 2008 6:47 AM
    • Marked as answer by Rain3204 Wednesday, July 2, 2008 6:47 AM
    • Marked as answer by Rain3204 Wednesday, July 2, 2008 6:48 AM
    Tuesday, July 1, 2008 2:11 PM

All replies

  • may i know What technology are you using 1.1 / 2.0
    Wife is like a Software, Lots of bugs.
    Tuesday, July 1, 2008 12:53 PM
  • You can select it with LINQ :

    using System.Xml.Linq; 
    XElement X_Element = XElement.Load("XmlFileName.xml"); 
     
    var node = (from tmp in X_Element.Descendants("Filename"
                where tmp.Attribute("name").Value == "3" 
                select tmp).First(); 
                 
    Console.write(node.Name); 
     

    Tuesday, July 1, 2008 1:00 PM
  • You can examine the ParentNode property of the XmlElement that represents <Filename name="3"/> and the ParentNode property of that and continue up until you get to the Document node (NodeType==Document).

    XmlElement e; // initiate it to the node you want the path of.
    string s="";
    while(XmlElement.Parent!=null){
      e=XmlElement.Parent;
      if (e.NodeType==NodeType.Document){
        break;
      }
      s=e.Name+"\\"+s;
    }

    Something like that. Note - Untested code.


    /Ruben RJJournal
    • Marked as answer by Rain3204 Wednesday, July 2, 2008 6:48 AM
    • Marked as answer by Rain3204 Wednesday, July 2, 2008 6:48 AM
    • Unmarked as answer by Rain3204 Wednesday, July 2, 2008 6:48 AM
    • Unmarked as answer by Rain3204 Wednesday, July 2, 2008 6:48 AM
    • Marked as answer by Rain3204 Wednesday, July 2, 2008 6:48 AM
    Tuesday, July 1, 2008 1:03 PM
  • Hi jack,

    Im using 2.0


    Thanks and Regards,
    Tuesday, July 1, 2008 1:54 PM
  • Hi Mohammad,

    Thanks for the suggestion, i really appreciate it but i would prefer using System.Xml since the rest of my code is already using it.



    Thanks and Regard,
    Tuesday, July 1, 2008 1:56 PM
  • I tried to solve ur query :
    There is button for which i have written an event handler
    I it i load xml document and call a user defined function GetParentNodes(...) :
    private void button1_Click(object sender, EventArgs e) 
            { 
                XmlDocument doc = new XmlDocument(); 
                doc.Load("test.xml"); 
                XmlElement root = doc.DocumentElement; 
                XmlNodeList nodeList = root.GetElementsByTagName("Filename"); 
                //string s; 
                XmlNode node; 
                for (int i = 0; i < nodeList.Count; i++) 
                { 
                    node = nodeList[i]; 
                    if (node.Attributes["name"].Value == "3") 
                    { 
                        foreach (string s in GetParentNodes(node)) 
                            Console.WriteLine(s); 
                        break; 
                    } 
                } 
                //Console.Write(doc.DocumentElement.Name); 
            } 
     
            private IEnumerable<string> GetParents(XmlNode node) 
            { 
                for (; ; ) 
                { 
                    nodenode = node.ParentNode; 
                    if (node == null || node.NodeType == XmlNodeType.Document) break; 
                    yield return node.Name; 
                } 
            } 


    Wife is like a Software, Lots of bugs.
    • Proposed as answer by Rain3204 Wednesday, July 2, 2008 6:47 AM
    • Marked as answer by Rain3204 Wednesday, July 2, 2008 6:47 AM
    • Marked as answer by Rain3204 Wednesday, July 2, 2008 6:48 AM
    Tuesday, July 1, 2008 2:11 PM
  • Hi,


    Thanks everyone. Works like charm. I really appreciate the help.


    Thanks and Regards,
    Wednesday, July 2, 2008 6:49 AM