Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析XML c#问题_C#_Xml_Parsing - Fatal编程技术网

解析XML c#问题

解析XML c#问题,c#,xml,parsing,C#,Xml,Parsing,问题是,当我单击按钮时,会显示一个空文本框。应该显示IP地址。XML响应如下所示 private void nsButton3_Click(object sender, EventArgs e) { string geoip = nsTextBox4.Text; WebClient wc = new WebClient(); string geoipxml = (wc.DownloadString("http://freeg

问题是,当我单击按钮时,会显示一个空文本框。应该显示IP地址。XML响应如下所示

        private void nsButton3_Click(object sender, EventArgs e)
    {
        string geoip = nsTextBox4.Text;
        WebClient wc = new WebClient();
        string geoipxml = (wc.DownloadString("http://freegeoip.net/xml/" + geoip));
        StringBuilder output = new StringBuilder();
        using (XmlReader reader = XmlReader.Create(new StringReader(geoipxml)))
        {
            reader.ReadToFollowing("Response");
            reader.MoveToFirstAttribute();
            string geoipanswer = reader.Value;
            MessageBox.Show(geoipanswer);
        }
    }
}
}

69.242.21.115
美国
美国
判定元件
特拉华州
威尔明顿
19805
39.7472
-75.5918
504
302

有什么想法吗?

Ip
是一个元素,您试图读取它,就像它是和属性一样:

<Response>
<Ip>69.242.21.115</Ip>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode>DE</RegionCode>
<RegionName>Delaware</RegionName>
<City>Wilmington</City>
<ZipCode>19805</ZipCode>
<Latitude>39.7472</Latitude>
<Longitude>-75.5918</Longitude>
<MetroCode>504</MetroCode>
<AreaCode>302</AreaCode>
</Response>
我建议您切换到LINQ到XML:

reader.MoveToFirstAttribute();

您需要
使用System.Xml.Linq
使其工作。

Ip
是一个元素,您试图读取它,就像它是和属性一样:

<Response>
<Ip>69.242.21.115</Ip>
<CountryCode>US</CountryCode>
<CountryName>United States</CountryName>
<RegionCode>DE</RegionCode>
<RegionName>Delaware</RegionName>
<City>Wilmington</City>
<ZipCode>19805</ZipCode>
<Latitude>39.7472</Latitude>
<Longitude>-75.5918</Longitude>
<MetroCode>504</MetroCode>
<AreaCode>302</AreaCode>
</Response>
我建议您切换到LINQ到XML:

reader.MoveToFirstAttribute();
您需要
使用System.Xml.Linq
使其正常工作