Reads the value of a node in an XML file.

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.