读者 XML 谷歌地球不起作用 C#

本文关键字:不起作用 谷歌地球 XML 读者 | 更新日期: 2023-09-27 18:32:36

我的 C# 中有一个代码,用于从 XML 文件中读取节点。但是使用下面的XML,我无法读取数据。这是一个 XML 导出谷歌地球我已经尝试了一切,但我无法让它工作......

我做错了什么?

法典:

public ActionResult Index()
{
    WebClient client = new WebClient();
    client.Encoding = System.Text.Encoding.UTF8;
    var xml = client.DownloadString(Server.MapPath("doc.xml"));
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    foreach (XmlNode node in doc.SelectNodes("Folder"))
    {
        string teste = node.SelectSingleNode("name").InnerText;
        foreach (XmlNode node1 in node.SelectNodes("Placemark"))
        {
            string teste1 = node.SelectSingleNode("name").InnerText;
        }
    }
    return View();
}

我更改的此代码:

foreach (XmlNode node in doc.SelectNodes("kml/Document/Folder"))
foreach (XmlNode node in doc.SelectNodes("Document/Folder"))
foreach (XmlNode node in doc.SelectNodes("kml"))

而且不工作。

.XML:

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Document>
    <name>Clientes</name>
    <open>1</open>
    <LookAt>
        <longitude>-49.0749402778</longitude>
        <latitude>-26.986807651</latitude>
        <altitude>0</altitude>
        <heading>0</heading>
        <tilt>0</tilt>
        <range>219018.609535</range>
    </LookAt>
    <Style id="pground_fresnel">
        <PolyStyle>
            <color>66ff00ff</color>
            <outline>0</outline>
        </PolyStyle>
    </Style>
    <Folder>
        <name>Sites</name>
        <Placemark id="site_174196976">
            <name>ERB - FTZA</name>
            <description><![CDATA[<p>Approximate profiles to nearby sites</p>

                         <ul>
                            <li>Profile between ERB - FTZA and <a href="#site_174641424">EFA - ST01 - Gilmar Salvador</a><br>
                            <img src="http://profile.heywhatsthat.com/bin/profile.cgi?src=MTPTP&metric=1&axes=1&curvature=1&pt1=-26.8616181581%2C-49.0435799235%2C%2C%2C&pt0=-26.8616887564%2C-49.0433335127%2C%2C%2C">
                            </li>
                         </ul>

                         <ul>
                            <li>Profile between ERB - FTZA and <a href="#site_174558608">EFA - PTP - VALMOR Têxtil</a><br>
                            <img src="http://profile.heywhatsthat.com/bin/profile.cgi?src=MTPTP&metric=1&axes=1&curvature=1&pt1=-26.8621335079%2C-49.0440949031%2C%2C%2C&pt0=-26.8616887564%2C-49.0433335127%2C%2C%2C">
                            </li>
                         </ul>

                         <ul>
                            <li>Profile between ERB - FTZA and <a href="#site_174686480">EFA - ST01 - Jussara Marcelino</a><br>
                            <img src="http://profile.heywhatsthat.com/bin/profile.cgi?src=MTPTP&metric=1&axes=1&curvature=1&pt1=-26.8608348824%2C-49.0445371763%2C%2C%2C&pt0=-26.8616887564%2C-49.0433335127%2C%2C%2C">
                            </li>
                         </ul>

                         <ul>
                            <li>Profile between ERB - FTZA and <a href="#site_174558064">EFA - ST01 - Fabricio Dalcenter</a><br>
                            <img src="http://profile.heywhatsthat.com/bin/profile.cgi?src=MTPTP&metric=1&axes=1&curvature=1&pt1=-26.8602120003%2C-49.0431030006%2C%2C%2C&pt0=-26.8616887564%2C-49.0433335127%2C%2C%2C">
                            </li>
                         </ul>

                         <ul>
                            <li>Profile between ERB - FTZA and <a href="#site_174686928">MCO - PTP - Celpa Defumados</a><br>
                            <img src="http://profile.heywhatsthat.com/bin/profile.cgi?src=MTPTP&metric=1&axes=1&curvature=1&pt1=-26.8582398311%2C-49.0353971392%2C%2C%2C&pt0=-26.8616887564%2C-49.0433335127%2C%2C%2C">
                            </li>
                         </ul>

                         <ul>
                            <li>Profile between ERB - FTZA and <a href="#site_114087920">MCO - PTP - Ananda Têxtil</a><br>
                            <img src="http://profile.heywhatsthat.com/bin/profile.cgi?src=MTPTP&metric=1&axes=1&curvature=1&pt1=-26.8569743348%2C-49.0334629119%2C%2C%2C&pt0=-26.8616887564%2C-49.0433335127%2C%2C%2C">
                            </li>
                         </ul>

                         <ul>
                            <li>Profile between ERB - FTZA and <a href="#site_174555568">Prédios Tribess</a><br>
                            <img src="http://profile.heywhatsthat.com/bin/profile.cgi?src=MTPTP&metric=1&axes=1&curvature=1&pt1=-26.8705460688%2C-49.0501338292%2C%2C%2C&pt0=-26.8616887564%2C-49.0433335127%2C%2C%2C">
                            </li>
                         </ul>

                         <ul>
                            <li>Profile between ERB - FTZA and <a href="#site_114081584">Rede Top Tribess</a><br>
                            <img src="http://profile.heywhatsthat.com/bin/profile.cgi?src=MTPTP&metric=1&axes=1&curvature=1&pt1=-26.8754375344%2C-49.0547021027%2C%2C%2C&pt0=-26.8616887564%2C-49.0433335127%2C%2C%2C">
                            </li>
                         </ul>

                         <ul>
                            <li>Profile between ERB - FTZA and <a href="#site_174690128">Estudo maion</a><br>
                            <img src="http://profile.heywhatsthat.com/bin/profile.cgi?src=MTPTP&metric=1&axes=1&curvature=1&pt1=-26.8711031328%2C-49.0686806897%2C%2C%2C&pt0=-26.8616887564%2C-49.0433335127%2C%2C%2C">
                            </li>
                         </ul>

                         <ul>
                            <li>Profile between ERB - FTZA and <a href="#site_114080784">Mafisa</a><br>
                            <img src="http://profile.heywhatsthat.com/bin/profile.cgi?src=MTPTP&metric=1&axes=1&curvature=1&pt1=-26.8625412632%2C-49.0864785295%2C%2C%2C&pt0=-26.8616887564%2C-49.0433335127%2C%2C%2C">
                            </li>
                         </ul>

            <em>
                Profile images are Copyright 2008 Michael Kosowsky.
                All rights reserved. Used with permission. For more
                information, visit
                <a href="http://www.heywhatsthat.com/">HeyWhatsThat</a>
                </em>]]></description>
            <styleUrl>#msn_donut</styleUrl>
            <Point>
                <altitudeMode>relativeToGround</altitudeMode>
                <coordinates>-49.0433335127,-26.8616887564,0</coordinates>
            </Point>
        </Placemark>
    </Folder>
    <Folder>
        <name>Links</name>
        <Folder>
            <name>ERB Gasparinho - ST01 - MPO to EGO - LAN - Jurema</name>
            <LookAt>
                <longitude>-48.9692241831</longitude>
                <latitude>-26.9532849988</latitude>
                <altitude>0</altitude>
                <heading>-0.8219678042850001</heading>
                <tilt>68</tilt>
                <range>18.4258281664</range>
            </LookAt>
            <Placemark>
                <name>Vertical Fresnel</name>
                <LookAt>
                    <longitude>-48.9692241831</longitude>
                    <latitude>-26.9532849988</latitude>
                    <altitude>0</altitude>
                    <heading>-0.8219678042850001</heading>
                    <tilt>68</tilt>
                    <range>18.4258281664</range>
                </LookAt>
                <styleUrl>#pvertical_fresnel</styleUrl>
                <Polygon>
                    <altitudeMode>absolute</altitudeMode>
                    <outerBoundaryIs>
                        <LinearRing>
                            <coordinates>
                                -48.969316956,-26.9532861916,76.299996104 -48.9693166688,-26.95328618790001,76.26002066620001 -48.9693158091,-26.9532861769,76.2002543831 -48.9693143821,-26.9532861585,76.12106573360001 -48.9693123968,-26.953286133,76.0229429422 -48.96930986530001,-26.9532861004,75.9064909685 -48.9693068032,-26.9532860611,75.7724277776 -48.9693032295,-26.9532860151,75.6215799136 -48.96929916610001,-26.9532859629,75.4548774039 -48.9692946381,-26.9532859047,75.2733480248 -48.9692896734,-26.9532858408,75.0781109653 -48.9692843027,-26.9532857718,74.8703699268 -48.9692785589,-26.9532856979,74.6514057018 -48.9692724777,-26.9532856197,74.4225682778 -48.9692660964,-26.9532855377,74.1852685134 -48.96925945440001,-26.95328545230001,73.9409694406 -48.9692525926,-26.9532853641,73.6911772444 -48.9692455533,-26.9532852736,73.4374319769 -48.96923838,-26.9532851813,73.18129806189999 -48.9692311169,-26.9532850879,72.9243546502 -48.96922380870001,-26.953284994,72.6681858834 -48.9692165005,-26.9532849,72.414371127 -48.9692092374,-26.9532848066,72.1644752336 -48.9692020642,-26.9532847144,71.92003889439999 -48.96919502500001,-26.9532846239,71.6825691406 -48.9691881632,-26.9532845356,71.4535300524 -48.9691815213,-26.9532844502,71.23433373170001 -48.96917514,-26.9532843682,71.02633159689999 -48.9691690588,-26.95328429,70.8308060502 -48.9691633152,-26.9532842161,70.6489625718 -48.9691579446,-26.9532841471,70.481922287 -48.96915298,-26.9532840833,70.3307150548 -48.96914845210001,-26.953284025,70.1962731181 -48.9691443888,-26.9532839728,70.079425356 -48.96914081520001,-26.9532839268,69.98089217400001 -48.9691377533,-26.9532838875,69.90128106189999 -48.9691352219,-26.9532838549,69.84108284849999 -48.9691332367,-26.9532838294,69.800668676 -48.9691318099,-26.953283811,69.7802877112 -48.96913095039999,-26.9532838,69.7800656094 -48.9691306633,-26.9532837963,69.79999610430001 -48.9691306633,-26.9532837963,69.80000389600001 -48.96913095039999,-26.9532838,69.8399666219 -48.9691318099,-26.953283811,69.8997279071 -48.9691332367,-26.9532838294,69.97891166780001 -48.9691352219,-26.9532838549,70.0770297098 -48.9691377533,-26.9532838875,70.1934771027 -48.96914081520001,-26.9532839268,70.3275359097 -48.9691443888,-26.9532839728,70.4783796135 -48.96914845210001,-26.953284025,70.6450782127 -48.96915298,-26.9532840833,70.82660395489999 -48.9691579446,-26.9532841471,71.0218376736 -48.9691633152,-26.9532842161,71.2295756879 -48.9691690588,-26.95328429,71.4485372239 -48.96917514,-26.9532843682,71.677372311 -48.9691815213,-26.9532844502,71.91467010469999 -48.9691881632,-26.9532845356,72.1589675852 -48.96919502500001,-26.9532846239,72.40875857739999 -48.9692020642,-26.9532847144,72.6625030366 -48.9692092374,-26.9532848066,72.91863654390001 -48.9692165005,-26.9532849,73.17557995110001 -48.96922380870001,-26.953284994,73.4317491166 -48.9692311169,-26.9532850879,73.68556467240001 -48.96923838,-26.9532851813,73.9354617611 -48.9692455533,-26.9532852736,74.17989968409999 -48.9692525926,-26.9532853641,74.4173714002 -48.96925945440001,-26.95328545230001,74.64641281750001 -48.9692660964,-26.9532855377,74.8656118194 -48.9692724777,-26.9532856197,75.0736169711 -48.9692785589,-26.9532856979,75.26914585180001 -48.9692843027,-26.9532857718,75.45099296079999 -48.9692896734,-26.9532858408,75.6180371503 -48.9692946381,-26.9532859047,75.7692485373 -48.96929916610001,-26.9532859629,75.9036948533 -48.9693032295,-26.9532860151,76.020547192 -48.9693068032,-26.9532860611,76.11908511999999 -48.96930986530001,-26.9532861004,76.1987011181 -48.9693123968,-26.953286133,76.2589043273 -48.9693143821,-26.9532861585,76.2993235747 -48.9693158091,-26.9532861769,76.3197096622 -48.9693166688,-26.95328618790001,76.3199369029 -48.969316956,-26.9532861916,76.3000038957 
                            </coordinates>
                        </LinearRing>
                    </outerBoundaryIs>
                </Polygon>
            </Placemark>
            <Placemark>
                <name>Ground Fresnel</name>
                <LookAt>
                    <longitude>-48.9692241831</longitude>
                    <latitude>-26.9532849988</latitude>
                    <altitude>0</altitude>
                    <heading>-0.8219678042850001</heading>
                    <tilt>68</tilt>
                    <range>18.4258281664</range>
                </LookAt>
                <styleUrl>#ground_fresnel</styleUrl>
                <Polygon>
                    <tessellate>1</tessellate>
                    <outerBoundaryIs>
                        <LinearRing>
                            <coordinates>
                                -48.969316956,-26.9532861916,0 -48.9693166731,-26.9532859176,0 -48.96931581770001,-26.9532856379,0 -48.969314395,-26.9532853542,0 -48.9693124139,-26.9532850683,0 -48.96930988639999,-26.953284782,0 -48.9693068283,-26.9532844969,0 -48.9693032583,-26.953284215,0 -48.96929919850001,-26.9532839378,0 -48.9692946739,-26.9532836671,0 -48.96928971240001,-26.9532834046,0 -48.9692843446,-26.953283152,0 -48.9692786036,-26.9532829106,0 -48.9692725247,-26.9532826822,0 -48.9692661455,-26.9532824679,0 -48.9692595053,-26.9532822693,0 -48.96925264499999,-26.9532820874,0 -48.969245607,-26.9532819235,0 -48.9692384345,-26.9532817785,0 -48.96923117189999,-26.9532816533,0 -48.9692238639,-26.9532815487,0 -48.9692165555,-26.9532814654,0 -48.9692092919,-26.9532814038,0 -48.9692021178,-26.9532813643,0 -48.96919507740001,-26.9532813473,0 -48.9691882142,-26.9532813527,0 -48.96918157040001,-26.9532813805,0 -48.969175187,-26.9532814307,0 -48.96916910350001,-26.9532815028,0 -48.9691633572,-26.9532815964,0 -48.96915798359999,-26.953281711,0 -48.9691530158,-26.9532818458,0 -48.96914848450001,-26.953282,0 -48.9691444177,-26.9532821727,0 -48.96914084029999,-26.9532823628,0 -48.9691377744,-26.9532825691,0 -48.969135239,-26.9532827903,0 -48.9691332496,-26.9532830251,0 -48.9691318186,-26.9532832721,0 -48.9691309547,-26.9532835297,0 -48.9691306633,-26.9532837963,0 -48.9691306633,-26.9532837963,0 -48.96913094599999,-26.9532840703,0 -48.96913180129999,-26.95328435,0 -48.96913322380001,-26.9532846336,0 -48.9691352049,-26.9532849195,0 -48.9691377322,-26.9532852059,0 -48.9691407902,-26.9532854909,0 -48.96914435999999,-26.9532857729,0 -48.96914841970001,-26.9532860501,0 -48.9691529442,-26.9532863207,0 -48.9691579056,-26.9532865832,0 -48.96916327330001,-26.9532868359,0 -48.9691690142,-26.9532870772,0 -48.969175093,-26.9532873057,0 -48.9691814721,-26.95328752,0 -48.9691881123,-26.9532877186,0 -48.9691949725,-26.9532879005,0 -48.9692020105,-26.9532880644,0 -48.969209183,-26.9532882094,0 -48.9692164456,-26.9532883346,0 -48.9692237536,-26.9532884392,0 -48.9692310619,-26.9532885226,0 -48.9692383255,-26.9532885842,0 -48.9692454997,-26.9532886236,0 -48.9692525401,-26.9532886407,0 -48.9692594034,-26.9532886353,0 -48.9692660472,-26.9532886074,0 -48.9692724307,-26.9532885573,0 -48.96927851430001,-26.9532884852,0 -48.9692842607,-26.9532883916,0 -48.9692896344,-26.953288277,0 -48.96929460230001,-26.9532881422,0 -48.96929913370001,-26.953287988,0 -48.9693032007,-26.9532878153,0 -48.96930677819999,-26.9532876252,0 -48.9693098442,-26.9532874189,0 -48.9693123798,-26.9532871977,0 -48.9693143693,-26.9532869628,0 -48.96931580039999,-26.9532867158,0 -48.96931666449999,-26.9532864583,0 -48.969316956,-26.9532861916,0 
                            </coordinates>
                        </LinearRing>
                    </outerBoundaryIs>
                </Polygon>
            </Placemark>
            <Placemark>
                <name>Ground</name>
                <LookAt>
                    <longitude>-48.9692241831</longitude>
                    <latitude>-26.9532849988</latitude>
                    <altitude>0</altitude>
                    <heading>-0.8219678042850001</heading>
                    <tilt>68</tilt>
                    <range>18.4258281664</range>
                </LookAt>
                <styleUrl>#ground_path_#ff0000</styleUrl>
                <LineString>
                    <tessellate>1</tessellate>
                    <coordinates>
                        -48.969316956,-26.9532861916,0 -48.96913141020001,-26.9532838059,0 
                    </coordinates>
                </LineString>
            </Placemark>
            <Placemark id="end_174725776">
                <name>ERB Gasparinho - ST01 - MPO</name>
                <description><![CDATA[<b>Product:</b> PTP58600<br>
                          <b>Pointing at:</b> <a href="#end_174725968">EGO - LAN - Jurema</a><br>
                          <b>Profile:</b> 0.0 kilometers, Line-of-Sight<br>
                          <b>Antenna:</b> Cambium Networks Integrated Dual Polar Antenna<br>
                          <b>Antenna height:</b> 10.0 m<br>
                          <b>Description:</b> <br>
                                  <img src="http://profile.heywhatsthat.com/bin/profile.cgi?src=MTPTP&metric=1&axes=1&curvature=1&pt1=-26.9532838059%2C-48.9691314102%2C%2C%2C&pt0=-26.9532861916%2C-48.969316956%2C%2C%2C"><br>

            <em>
                Profile images are Copyright 2008 Michael Kosowsky.
                All rights reserved. Used with permission. For more
                information, visit
                <a href="http://www.heywhatsthat.com/">HeyWhatsThat</a>
                </em>]]></description>
                <LookAt>
                    <longitude>-48.969316956</longitude>
                    <latitude>-26.9532861916</latitude>
                    <altitude>10</altitude>
                    <heading>85.1780321957</heading>
                    <tilt>68</tilt>
                    <range>101.842582817</range>
                </LookAt>
                <styleUrl>#antenna</styleUrl>
                <Point>
                    <extrude>1</extrude>
                    <altitudeMode>relativeToGround</altitudeMode>
                    <coordinates>-48.969316956,-26.9532861916,9.999999999999998</coordinates>
                </Point>
            </Placemark>
            <Placemark id="end_174725968">
                <name>EGO - LAN - Jurema</name>
                <description><![CDATA[<b>Product:</b> PTP58600<br>
                          <b>Pointing at:</b> <a href="#end_174725776">ERB Gasparinho - ST01 - MPO</a><br>
                          <b>Profile:</b> 0.0 kilometers, Line-of-Sight<br>
                          <b>Antenna:</b> Cambium Networks Integrated Dual Polar Antenna<br>
                          <b>Antenna height:</b> 10.0 m<br>
                          <b>Description:</b> <br>
                                  <img src="http://profile.heywhatsthat.com/bin/profile.cgi?src=MTPTP&metric=1&axes=1&curvature=1&pt1=-26.9532861916%2C-48.969316956%2C%2C%2C&pt0=-26.9532838059%2C-48.9691314102%2C%2C%2C"><br>

            <em>
                Profile images are Copyright 2008 Michael Kosowsky.
                All rights reserved. Used with permission. For more
                information, visit
                <a href="http://www.heywhatsthat.com/">HeyWhatsThat</a>
                </em>]]></description>
                <LookAt>
                    <longitude>-48.9691314102</longitude>
                    <latitude>-26.9532838059</latitude>
                    <altitude>10</altitude>
                    <heading>265.177948095</heading>
                    <tilt>68</tilt>
                    <range>101.842582817</range>
                </LookAt>
                <styleUrl>#antenna</styleUrl>
                <Point>
                    <extrude>1</extrude>
                    <altitudeMode>relativeToGround</altitudeMode>
                    <coordinates>-48.96913141020001,-26.9532838059,9.999999999999998</coordinates>
                </Point>
            </Placemark>
            <Folder>
                <name>Ticks</name>
            </Folder>
        </Folder>
    </Folder>
</Document>
</kml>

读者 XML 谷歌地球不起作用 C#

这是因为此 XML 具有默认的命名空间xmlns="http://www.opengis.net/kml/2.2"

System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);
xmlnsManager.AddNamespace("ns", "http://www.opengis.net/kml/2.2");
foreach (XmlNode node in doc.SelectNodes("ns:kml/ns:Document/ns:Folder"))

根据请求更改并返回错误:

Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.

法典:

public ActionResult Index()
{
    WebClient client = new WebClient();
    client.Encoding = System.Text.Encoding.UTF8;
    var xml = client.DownloadString(Server.MapPath("doc.xml"));
    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    System.Xml.XmlNamespaceManager xmlnsManager = new System.Xml.XmlNamespaceManager(doc.NameTable);
    xmlnsManager.AddNamespace("ns", "http://www.opengis.net/kml/2.2");
    foreach (XmlNode node in doc.SelectNodes("ns:kml/ns:Document/ns:Folder"))
    {
        string teste = node.SelectSingleNode("name").InnerText;
        foreach (XmlNode node1 in node.SelectNodes("Placemark"))
        {
            string teste1 = node.SelectSingleNode("name").InnerText;
        }
    }
    return View();
}