如何获取名称中包含 : 的元素?

发布于 2024-10-17 04:24:39 字数 1490 浏览 1 评论 0原文

我需要从此 XML 获取 CountryName: http://api.hostip.info/?ip =12.215.42.19

响应 XML 为:

<HostipLookupResultSet version="1.0.1"
  xmlns:gml="http://www.opengis.net/gml"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd">

  <gml:description>This is the Hostip Lookup
  Service</gml:description>
  <gml:name>hostip</gml:name>
  <gml:boundedBy>
    <gml:Null>inapplicable</gml:Null>
  </gml:boundedBy>
  <gml:featureMember>
    <Hostip>
      <ip>12.215.42.19</ip>
      <gml:name>Sugar Grove, IL</gml:name>
      <countryName>UNITED STATES</countryName>
      <countryAbbrev>US</countryAbbrev>
      <!-- Co-ordinates are available as lng,lat -->
      <ipLocation>
        <gml:pointProperty>
          <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">

            <gml:coordinates>-88.4588,41.7696</gml:coordinates>
          </gml:Point>
        </gml:pointProperty>
      </ipLocation>
    </Hostip>
  </gml:featureMember>
</HostipLookupResultSet>

问题是我无法在 后代 方法中包含 :,因为它会抛出:

XmlException:“:”字符, 十六进制值 0x3A,不能 包含在名称中。

谢谢

I need to get the CountryName from this XML: http://api.hostip.info/?ip=12.215.42.19

The response XML is:

<HostipLookupResultSet version="1.0.1"
  xmlns:gml="http://www.opengis.net/gml"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:noNamespaceSchemaLocation="http://www.hostip.info/api/hostip-1.0.1.xsd">

  <gml:description>This is the Hostip Lookup
  Service</gml:description>
  <gml:name>hostip</gml:name>
  <gml:boundedBy>
    <gml:Null>inapplicable</gml:Null>
  </gml:boundedBy>
  <gml:featureMember>
    <Hostip>
      <ip>12.215.42.19</ip>
      <gml:name>Sugar Grove, IL</gml:name>
      <countryName>UNITED STATES</countryName>
      <countryAbbrev>US</countryAbbrev>
      <!-- Co-ordinates are available as lng,lat -->
      <ipLocation>
        <gml:pointProperty>
          <gml:Point srsName="http://www.opengis.net/gml/srs/epsg.xml#4326">

            <gml:coordinates>-88.4588,41.7696</gml:coordinates>
          </gml:Point>
        </gml:pointProperty>
      </ipLocation>
    </Hostip>
  </gml:featureMember>
</HostipLookupResultSet>

Problem is I can't include : in the Descendants method because it throws:

XmlException: The ':' chracater,
hexadecimal value 0x3A, cannot be
included in a name.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

海之角 2024-10-24 04:24:39

尝试这个

var descendants = from i in XDocument.Load(xml).Descendants("Hostip")
select i.Element("countryName");

更新

完整代码来下载xml并查找countryName的名称

string xml;
using(var web = new WebClient())
{
xml = web.DownloadString("http://api.hostip.info/?ip=12.215.42.19");
}
var descendants = from i in XDocument.Parse(xml).Descendants("Hostip")
select i.Element("countryName");

try this

var descendants = from i in XDocument.Load(xml).Descendants("Hostip")
select i.Element("countryName");

Update

complete code for downloading the xml and finding the name of countryName

string xml;
using(var web = new WebClient())
{
xml = web.DownloadString("http://api.hostip.info/?ip=12.215.42.19");
}
var descendants = from i in XDocument.Parse(xml).Descendants("Hostip")
select i.Element("countryName");
青巷忧颜 2024-10-24 04:24:39

有关如何在 LINQ to XML 中应用命名空间的小示例:

XElement doc = XElement.Load("test.xml");
XNamespace ns = "http://www.opengis.net/gml";

var firstName = doc.Descendants(ns + "name").First().Value;

A small example on how to apply namespaces in LINQ to XML:

XElement doc = XElement.Load("test.xml");
XNamespace ns = "http://www.opengis.net/gml";

var firstName = doc.Descendants(ns + "name").First().Value;
温柔女人霸气范 2024-10-24 04:24:39

您需要引用 gml 命名空间;完成后,您应该能够使用“gml:”右侧显示的标签名称进行导航

更新

我不确定您要将其应用到什么上下文,但这里是一个可以运行的示例控制台应用程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace LinqToXmlSample
{
    class Program
    {
        static void Main(string[] args)
        {
            XElement x = XElement.Load("http://api.hostip.info/?ip=12.215.42.19");
            foreach (XElement hostip in x.Descendants("Hostip"))
            {
                string country = Convert.ToString(hostip.Element("countryName").Value);
                Console.WriteLine(country);
            }
            Console.ReadLine();
        }
    }
}

You need to reference the gml namespace; once you've done that you should be able to navigate using the tag names that appear to the right of "gml:"

UPDATE

I'm not sure what context you're applying this to, but here's a sample console app that works:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;

namespace LinqToXmlSample
{
    class Program
    {
        static void Main(string[] args)
        {
            XElement x = XElement.Load("http://api.hostip.info/?ip=12.215.42.19");
            foreach (XElement hostip in x.Descendants("Hostip"))
            {
                string country = Convert.ToString(hostip.Element("countryName").Value);
                Console.WriteLine(country);
            }
            Console.ReadLine();
        }
    }
}
阳光下的泡沫是彩色的 2024-10-24 04:24:39
var gml = (XNamespace)"http://www.opengis.net/gml";
var doc = XDocument.Load(...) or XDocument.Parse(...);
var name = doc.Descendants(gml + "featureMember").Descendants("countryName").First().Value;

或者你可以使用暴力并删除所有名称空间:

void RemoveNamespace(XDocument xdoc)
{
    foreach (XElement e in xdoc.Root.DescendantsAndSelf())
    {
        if (e.Name.Namespace != XNamespace.None)
        {
            e.Name = XNamespace.None.GetName(e.Name.LocalName);
        }

        if (e.Attributes().Any(a => a.IsNamespaceDeclaration || a.Name.Namespace != XNamespace.None))
        {
            e.ReplaceAttributes(e.Attributes().Select(a => a.IsNamespaceDeclaration ? null : a.Name.Namespace != XNamespace.None ? new XAttribute(XNamespace.None.GetName(a.Name.LocalName), a.Value) : a));
        }
    }
}
var gml = (XNamespace)"http://www.opengis.net/gml";
var doc = XDocument.Load(...) or XDocument.Parse(...);
var name = doc.Descendants(gml + "featureMember").Descendants("countryName").First().Value;

Or you could go brute force and strip all the namespaces:

void RemoveNamespace(XDocument xdoc)
{
    foreach (XElement e in xdoc.Root.DescendantsAndSelf())
    {
        if (e.Name.Namespace != XNamespace.None)
        {
            e.Name = XNamespace.None.GetName(e.Name.LocalName);
        }

        if (e.Attributes().Any(a => a.IsNamespaceDeclaration || a.Name.Namespace != XNamespace.None))
        {
            e.ReplaceAttributes(e.Attributes().Select(a => a.IsNamespaceDeclaration ? null : a.Name.Namespace != XNamespace.None ? new XAttribute(XNamespace.None.GetName(a.Name.LocalName), a.Value) : a));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文