It is known that there is a local xml file (D: \ location. xml) as follows:
<? 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 coordinate information that shows the geographical location. This xml document requires that you obtain the value, lat, and lng under the <location> node, that is, the longitude and latitude.
I haven't practiced xml for a long time, and I am not familiar with xml. I have found some xml knowledge for hundreds of times. The following example shows how I can implement the obtained (including) step by step ):
First, instantiate xml to obtain the overall information of the xml file. This is required. The following code is the first time I wrote it. I didn't get it. I posted it for you to see why.
// Instantiate xml XmlDocument xml = new XmlDocument (); // read xml file. 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 my most stupid method. One by one, the node is obtained step by step, and the result is not as expected, so I did something thankless. So ah, work requires efficiency, skill, and no brute force. We do not recommend that you waste time thinking about this.
If there is a problem, you have to find out the problem and solve the problem. Now I have improved the previous acquisition method and the result is still not obtained. Now I am depressed and feel that there is nothing wrong with writing. How can I not get it? And give you a look
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 is not a best bet. No, hurry and change your mind, change your thinking, and solve the problem. You cannot sink yourself into a problem and solidify your thinking, so when you get stuck, you need to relax now, either to get a cup of water to drink, or to breathe out, or simply to get WC. How can we get comfortable... This is often the case with inspiration...
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! = "") {// Here is the value you want Console. WriteLine (lat); Console. WriteLine (lng) ;}} Console. Read ();
This code is the result I want. Everything is OK !!!
There are a lot of things that you forget, learning is to keep learning from forgetting and picking up.
For reference only !!!