I have an xml file, what I am trying to do is to parse the complete file and search for a specific xml tag (in my case I am searching for queryString
) and when the tag is encountered pull out the inner text corresponding to it. I am using XmlDocument
and using XmlDocument.SelectNodes("/stringList")
.
While doing so a null
value is being returned. Am I missing out on something?
XmlDocument xml = new XmlDocument();
Jrxml.Load(file_path);
XmlNodeList xml_nodes = xml.SelectNodes("/stringList");
foreach (XmlNode jr_node in xml_nodes)
{
XmlNode query_node = jr_node.SelectSingleNode("queryString");
}
While execution it does not enter the for loop as xml_nodes
value is null
Xml File looks like this.
<stringList>
<property1/>
<property2/>
<style>
<queryString>
</queryString>
</style>
<queryString>
</queryString>
</stringList>
If you're searching only for "queryString" tag I suggest you to use XmlDocument method GetElementsByTagName. Consider:
using System;
using System.Xml;
namespace TestCon
{
class Program
{
private static XmlDocument TestDoc;
public static void Main(string[] args)
{
TestDoc = new XmlDocument();
TestDoc.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?>"+
"<stringList>\n"+
"<property1/>\n"+"<property2/>\n"+
"<style>\n"+"<queryString>Who's on fist."+"</queryString>\n"+
"</style>\n"+"<queryString>Who's on second."+"</queryString>\n"+
"</stringList>");
XmlNodeList elemList = TestDoc.GetElementsByTagName("queryString");
foreach (XmlNode foundNode in elemList)
{
Console.WriteLine(foundNode.InnerText);
}
Console.Write("Press any key to continue . . . ");
Console.ReadKey(true);
}
}
}
You will get exactly two nodes you're searching for:
Who's on first.
Who's on second.
Press any key to continue . . .