【问题标题】:How to extract a value of a http xml answer如何提取http xml答案的值
【发布时间】:2012-09-20 00:14:24
【问题描述】:

好吧,我对 C# 有点陌生,我正在尝试获取 http 答案的值。但我以前从未使用过那些 XML 的东西。

简单示例:http://freegeoip.net/xml/123.123.123.123

<Response>
<Ip>123.123.123.123</Ip>
<CountryCode>CN</CountryCode>
<CountryName>China</CountryName>
<RegionCode>22</RegionCode>
<RegionName>Beijing</RegionName>
<City>Beijing</City>
<ZipCode/>
<Latitude>39.9289</Latitude>
<Longitude>116.388</Longitude>
<MetroCode/>
</Response>

我想在 C# 中返回 &lt;CountryName&gt;&lt;/CountryName&gt; 部分。 有什么例子吗?

【问题讨论】:

  • 搜索 linq to xml。你会发现有用的例子
  • 查看您的 c# 代码以了解您如何获得该响应可能很有用。如果它正在处理 XML,您是否尝试过使用谷歌搜索 c# xml 示例?例如。 csharp-examples.net/xml-nodes-by-name希望有帮助。

标签: c# xml parsing


【解决方案1】:

您可以通过以下两种方式之一进行操作。快速而肮脏的方法是在 XML 字符串中简单地搜索 CountryName,但我怀疑这是一个特别可靠的答案。

由于提供的 XML 格式不正确这一事实证明此响应,我将提供以编程方式读取 XML 的更好答案是在 System.Xml 命名空间中,并使用 XmlDocument 对象加载和解析数据:

using System.Xml;

public static void Main(String args[])
{
    XmlDocument foo = new XmlDocument();

    //Let's assume that the IP of the target player is in args[1]
    //This allows us to parameterize the Load method to reflect the IP address
    //of the user per the OP's request
    foo.Load( String.Format("http://freegeoip.net/xml/{0}",args[1])); 

    XmlNode root = foo.DocumentElement;

    // you might need to tweak the XPath query below
    XmlNode countryNameNode = root.SelectSingleNode("/Response/CountryName");

    Console.WriteLine(countryNameNode.InnerText);
}

这不是一个 100% 完美的解决方案,但它应该代表一个良好的开端。希望这会有所帮助。

【讨论】:

  • 如果我使用定义的 IP,它确实有效。但是如何在 Load 中使用 2 个参数?例如> XmlDocument locrequest = new XmlDocument(); > locrequest.Load("freegeoip.net/xml{0}", player.IP); > XmlNode 根 = locrequest.DocumentElement; > 字符串来自 = root.SelectSingleNode("CountryName").InnerText;
  • @IngoGuther 查看修改后的答案以确定我是否回答了您的问题。
  • 我把最后一条评论搞砸了……我该如何格式化? ``不起作用
  • 在 cmets 中格式化代码块比较困难。不用担心,我想我明白你在问什么。
  • 是的,这就是我要找的 (String.Format)。谢谢!
【解决方案2】:

使用XmlDocument.Load Method 从 URL 中获取文档。

然后使用XmlNode.SelectNodes Method 获取您感兴趣的节点。为此,您需要使用简单的XPath Expression

您也可以使用LINQ2XML

【讨论】:

    【解决方案3】:

    试试这个程序。然后就可以通过响应对象访问信息了。

    public class Response
    {
        public string Ip { get; set; }
        public string Countrycode { get; set; }
        public string CountryName { get; set; }
        public string RegionCode { get; set; }
        public string City { get; set; }
        public string ZipCode { get; set; }
        public string Latitude { get; set; }
        public string Longitude { get; set; }
        public string MetroCode { get; set; }
    }
    
    class Program
    {
        static void Main(string[] args)
        {
            var client = new System.Net.WebClient();
    
            string downloadedString = client.DownloadString("http://freegeoip.net/xml/123.123.123.123");
    
            XmlSerializer mySerializer =
                new XmlSerializer(typeof(Response));
    
            Response response = null;
    
            XmlReader xmlReader = XmlReader.Create(new System.IO.StringReader(downloadedString));
    
            response = (Response)mySerializer.Deserialize(xmlReader);
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-01
      相关资源
      最近更新 更多