Click here to Skip to main content
15,908,112 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
<Locations>
<North>
<DHI>
<METRO>
<DETAILS>
<CItyname>
<cityid></cityid>
</CItyname>

So i want the inner text of the selected node of the above xml so i am using the following

syntax

C#
var nodeList = doc.SelectNodes(string.Format("/Locations/" + Session["North"].ToString() + "/" + Session["DHI"].ToString() + "/" + Session["METRO"].ToString() + "/DETAILS/"  "/" + cityid));


I tried as above but i am getting null vaule in node list


C#
XmlNodeList xnList = doc.SelectNodes("/Locations");
      foreach (XmlNode xn in xnList)
      {
          XmlNode anode = doc.SelectSingleNode(string.Format( "North"));
          if (anode != null)
          {
              XmlNodeList CNodes = xn.SelectNodes("DHI/METRO/DETAILS/CItyname");
              foreach (XmlNode node in CNodes)
              {
                  XmlNode example = node.SelectSingleNode("cityid");
                  if (example != null)
                  {
                      string na = example.InnerText;

                  }
              }
          }
      }

I have tried above way aslo but in this first node is reading and second node is coming as null

I want the Inner text of the CITyID so please help me guys ....

What I have tried:

C#
var nodeList = doc.SelectNodes(string.Format("/Locations/" + Session["North"].ToString() + "/" + Session["DHI"].ToString() + "/" + Session["METRO"].ToString() + "/DETAILS/"  "/" + cityid));


I tried as above but i am getting null vaule in node list


C#
XmlNodeList xnList = doc.SelectNodes("/Locations");
      foreach (XmlNode xn in xnList)
      {
          XmlNode anode = doc.SelectSingleNode(string.Format( "North"));
          if (anode != null)
          {
              XmlNodeList CNodes = xn.SelectNodes("DHI/METRO/DETAILS/CItyname");
              foreach (XmlNode node in CNodes)
              {
                  XmlNode example = node.SelectSingleNode("cityid");
                  if (example != null)
                  {
                      string na = example.InnerText;

                  }
              }
          }
      }
Posted
Updated 1-Apr-16 3:34am

In the first example you have missed the level CItyname and you are not using string.Format correctly.

In the second attempt you are attempting to select nodes from the wrong objects in the following lines:
C#
XmlNode anode = doc.SelectSingleNode(string.Format( "North"));
should be
C#
XmlNode anode = xn.SelectSingleNode("North");

(no need for the string.format) and similarly
XmlNodeList CNodes = xn.SelectNodes("DHI/METRO/DETAILS/CItyname");
should be
XmlNodeList CNodes = anode.SelectNodes("DHI/METRO/DETAILS/CItyname");
 
Share this answer
 
Comments
manoj1412012 5-Apr-16 3:37am    
Thanks Chill60
Is the structure fixed? That is, do you always have CityName at the end of same list of node paths?

If so, just use //CityName in SelectNodes - it will return nodes CityName wherever they are in the document.

Other then that, you use String.Format without formatting (in the second example string.Format("North") in the first
String.Format(long concatenation) in the other example

Try instead:
C#
String.Format("/{0}/{1}/{2}/{3}/DETAILS", "Locations", Session["North"], Session["DHI"], Session["METRO"])


Also note that XML is case sensitive in searches...so check that North shouldn't be NORTH or something like that.
 
Share this answer
 
Comments
manoj1412012 5-Apr-16 3:37am    
Thanks Sinisa Hajnal

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900