解析XML c#问题
问题是,当我单击按钮时,会显示一个空文本框。应该显示IP地址。XML响应如下所示解析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
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
使其正常工作