简体   繁体   中英

Pull specific xml tag in a xml file using XmlDocument

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 . . .

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  ...
}

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")
            });

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM