Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. XmlNode.Value Property

XmlNode.Value Property

Scheduled Pinned Locked Moved C#
xmlquestion
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • E Offline
    E Offline
    eggie5
    wrote on last edited by
    #1

    I have a piece of code that recursively goes though all internal nodes of an xml document and prints out the Name of each node. However, whenever I try to print out the Value of the node, it is always null. Why is it always null?

    #region Using directives

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Xml;

    #endregion

    namespace ConsoleApplication1
    {
    class Program
    {
    static void Main(string[] args)
    {
    try
    {
    XmlDocument doc = new XmlDocument();
    doc.Load("C:\\store.xml");
    XmlElement root = doc.DocumentElement;
    LoopThroughChildren(root);

                Console.Read();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
    
        }
    
        public static void LoopThroughChildren(XmlNode root)
        {
            Console.WriteLine(root.Name + " - " + root.Value); //root.Value is always null....
    
            foreach (XmlNode n in root.ChildNodes)
            {
                if (n.NodeType == XmlNodeType.Element)
                {
                    LoopThroughChildren(n);
                }
            }
        }
    }
    

    }

    /\ |_ E X E GG

    C 1 Reply Last reply
    0
    • E eggie5

      I have a piece of code that recursively goes though all internal nodes of an xml document and prints out the Name of each node. However, whenever I try to print out the Value of the node, it is always null. Why is it always null?

      #region Using directives

      using System;
      using System.Collections.Generic;
      using System.Text;
      using System.Xml;

      #endregion

      namespace ConsoleApplication1
      {
      class Program
      {
      static void Main(string[] args)
      {
      try
      {
      XmlDocument doc = new XmlDocument();
      doc.Load("C:\\store.xml");
      XmlElement root = doc.DocumentElement;
      LoopThroughChildren(root);

                  Console.Read();
              }
              catch (Exception e)
              {
                  Console.WriteLine(e.ToString());
              }
      
          }
      
          public static void LoopThroughChildren(XmlNode root)
          {
              Console.WriteLine(root.Name + " - " + root.Value); //root.Value is always null....
      
              foreach (XmlNode n in root.ChildNodes)
              {
                  if (n.NodeType == XmlNodeType.Element)
                  {
                      LoopThroughChildren(n);
                  }
              }
          }
      }
      

      }

      /\ |_ E X E GG

      C Offline
      C Offline
      ChrisAdams
      wrote on last edited by
      #2

      When an xml node with text is loaded up into a dom, the text is not added as part of the element, but is added as a seperate child node of type text. So something which looks like Hello in an xml file, would be represented within the dom as Element Node - Name: MyNode ---- Text Node - Value Hello. So when looping round the dom, to get the value of the MyNode element you need to access to first child and get it's value. root.firstChild.Value. Or use the most helpful Text property instead root.Text. Hope this helps....

      E 2 Replies Last reply
      0
      • C ChrisAdams

        When an xml node with text is loaded up into a dom, the text is not added as part of the element, but is added as a seperate child node of type text. So something which looks like Hello in an xml file, would be represented within the dom as Element Node - Name: MyNode ---- Text Node - Value Hello. So when looping round the dom, to get the value of the MyNode element you need to access to first child and get it's value. root.firstChild.Value. Or use the most helpful Text property instead root.Text. Hope this helps....

        E Offline
        E Offline
        eggie5
        wrote on last edited by
        #3

        Ah, thank you. I should have realized that. Also, you seem knowledgable about the xml dom. I have another question for you. Do you think it is wise to load a 50MB xml file into the dom? Is that pushing the limits? I do this at home and it can load it all in about 5 seconds, but here at work on a slower computer, it takes too long to wait for. I don't think I really need to use the dom eather, I'm not using any special funcitons from it. I think I should use the XmlTextReader instaed. What do you think? /\ |_ E X E GG

        1 Reply Last reply
        0
        • C ChrisAdams

          When an xml node with text is loaded up into a dom, the text is not added as part of the element, but is added as a seperate child node of type text. So something which looks like Hello in an xml file, would be represented within the dom as Element Node - Name: MyNode ---- Text Node - Value Hello. So when looping round the dom, to get the value of the MyNode element you need to access to first child and get it's value. root.firstChild.Value. Or use the most helpful Text property instead root.Text. Hope this helps....

          E Offline
          E Offline
          eggie5
          wrote on last edited by
          #4

          I want to use the XmlTextReader instead of the XmlDocument. How can I use the below method with the XmlTextReader?

          	public void LoopThroughChildren(XmlNode root)
          	{
          		this.listBox1.Items.Add(root.Name+ "  \\t\\t\\t"+root.FirstChild.Value);
          
          		foreach (XmlNode n in root.ChildNodes)
          		{
          			if (n.NodeType == XmlNodeType.Element)
          			{
          				LoopThroughChildren(n);
          			}
          		}
          
                  
          	}
          

          I can't get an XmlNode out of the XmlTextReader.... /\ |_ E X E GG

          C 1 Reply Last reply
          0
          • E eggie5

            I want to use the XmlTextReader instead of the XmlDocument. How can I use the below method with the XmlTextReader?

            	public void LoopThroughChildren(XmlNode root)
            	{
            		this.listBox1.Items.Add(root.Name+ "  \\t\\t\\t"+root.FirstChild.Value);
            
            		foreach (XmlNode n in root.ChildNodes)
            		{
            			if (n.NodeType == XmlNodeType.Element)
            			{
            				LoopThroughChildren(n);
            			}
            		}
            
                    
            	}
            

            I can't get an XmlNode out of the XmlTextReader.... /\ |_ E X E GG

            C Offline
            C Offline
            ChrisAdams
            wrote on last edited by
            #5

            It really depends on what you are trying to do with the xml document. A 50 MB document loaded into the DOM is quite big, but if you want to edit it then you will have to. From what I cal tell though all you really want to do is read the document and add the element's name and text value to a list. The XmlTextReader is ideal for this, as it is only reading a stream, and not creating objects in memory for each node, However this is not going to return you XmlNode's as it is not a dom implementation. Take a look at the documentation, you will need to call Read() to move along each element, then you can access the Name and Value property of the XmlTextReader to get the data you want I think.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups