The value of a node in the XML file to read

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

Known to have a local XML file (D:\location.xml).:

<?xml version="1.0" encoding="utf-8"?>
<GeocoderSearchResponse>
  <status>0</status>
  <result>
    <location>
      <lat>34.777046099314</lat>
      <lng>113.67314082709</lng>
    </location>
    <precise>1</precise>
    <confidence>80</confidence>
    <level>Business building</level>
  </result>
</GeocoderSearchResponse>

This is a display of geographic coordinate information, this XML document for <location> node values, lat and LNG, is the longitude and latitude.

Long time no practice over XML, XML also strange to many. On Baidu found some knowledge of XML, following the example about how I step by step to achieve access to the (including):

First of all, to instantiate XML, overall information for the file XML, this is a must. The following code is the first time I write, no access to, put out for everyone to have a look, did not find the reason.

            //The instantiation of XML
            XmlDocument xml = new XmlDocument();
            //Read the XML file
              xml.Load(@"D:\location.xml");  //Your XML address
              string lat = "";
            string lng = "";
            foreach (XmlNode node in xml.ChildNodes)
            {
                if (node.Name == "GeocoderSearchResponse")
                {
                    foreach (XmlNode node1 in node.ChildNodes)
                    {
                        if (node1.Name == "result")
                        {
                            foreach(XmlNode node2 in node.ChildNodes)
                            {
                                if (node2.Name == "location")
                                {
                                    foreach (XmlNode node3 in node2.ChildNodes)
                                    {
                                        switch (node3.Name)
                                        {
                                            case "lat":
                                                lat = node2.InnerText;
                                                break;
                                            case "lng":
                                                lng = node2.InnerText;
                                                break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }

 This is the way I the most stupid, one by one, gradual acquisition node, not willing, should do a thankless task. So ah, that is the need of work efficiency, to correct, not brute force. This does not suggest that you waste time trying to figure out.

There is a problem, it would have to identify problems, solve problems. Below I and improved access to the front, there is no access to, at this time I feel depression, write nothing wrong ah, how not to get? For everyone to see,

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(@"D:\location.xml");
            XmlNode xnRoot = xmldoc.SelectSingleNode("GeocoderSearchResponse");
            XmlNodeList nodelist = xnRoot.ChildNodes;
            foreach (XmlNode node in nodelist)
            {
                string lat = node.SelectSingleNode("lat").InnerXml;
                if (lat != "")
                {
                    Console.WriteLine(lat);
                }
            }

 Work, not into a dead end, no, hurriedly change, change idea, change idea, change the solution, can not sink to a problem with his ideas to the solidification lived, so when you get stuck, then you need to relax, to take a drink of water or air, either go out or just a trip to the WC, but how to how comfortable, ha ha... Often inspired really came...

            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(@"D:\location.xml");
            XmlNodeList xnRoot = xmldoc.SelectNodes("/GeocoderSearchResponse/result/location");
            //XmlNodeList nodelist = xnRoot.ChildNodes;
            foreach (XmlNode node in xnRoot)
            {
                string lat = node.SelectSingleNode("lat").InnerText.ToString();
                string lng = node.SelectSingleNode("lng").InnerText.ToString();
                if (lat != "" && lng != "")
                {
                    //This is the value you want
                    Console.WriteLine(lat);
                    Console.WriteLine(lng);
                }
            }

            Console.Read();

 This code is what I want results, all OK!!!

There are a lot of things are forgotten, learning is constantly forget free and picked up the room.

Statements of a school, for reference only!!!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Lillian at November 12, 2013 - 6:50 PM