XmlNode.Value Property
-
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 theValue
of the node, it is alwaysnull
. Why is it alwaysnull
?#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
-
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 theValue
of the node, it is alwaysnull
. Why is it alwaysnull
?#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
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....
-
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....
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
-
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....
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
-
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
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.