-1

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>
Jilna
  • 190
  • 1
  • 13

3 Answers3

1

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 . . .
kkomash
  • 91
  • 1
  • 1
  • 3
0

I prefer the XML classes/functionality found in System.Xml.Linq:

XDocument doc = XDocument.Parse(xmlString);

foreach (XElement queryString in doc.Descendants("queryString"))
{
    // do something with queryString.Value  ...
}
Fredrik
  • 2,247
  • 18
  • 21
0

you can use xml linq like this

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml =
                "<root>" +
                "<stringList>" +
                    "<property1/>" +
                    "<property2/>" +
                       "<style>" +
                         "<queryString>" +
                         "</queryString>" +
                       "</style>" +
                    "<queryString>" +
                    "</queryString>" +
                "</stringList>" +
                "</root>";
            XDocument doc = XDocument.Parse(xml);

            var stringList = doc.Descendants("stringList").Select(x => new
            {
                property1 = x.Element("property1"),
                property2 = x.Element("property2"),
                style = x.Element("style"),
                queryString = x.Element("queryString")
            });

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • Elements are predefined in this, whereas my requirement is that the required tag need not follow the same structure. – Jilna Apr 06 '16 at 12:46