我想从xml文件中获取纬度(lat)和经度(lon)值。
使用以下代码生成XML文件抛出Web请求。
代码
var request = WebRequest.Create("#") as HttpWebRequest;
request.Credentials = new System.Net.NetworkCredential("#", "#");
var response = request.GetResponse();
Stream receiveStream = response.GetResponseStream();
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
var result = readStream.ReadToEnd();
String xmlString = result;
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(new System.IO.StringReader(xmlString));
reader.Read();
System.Data.DataSet ds = new System.Data.DataSet();
ds.ReadXml(reader, System.Data.XmlReadMode.Auto);
DataTable dt = ds.Tables[0];
lbllat.Text = dt.Rows[0][5].ToString();//for example
lbllong.Text = dt.Rows[0][6].ToString();
但代码提供了以下错误信息。
错误:'>'是一个意外的令牌。预期的标记是'='。第2行,第483位
主要是因为XML标记不接受空格而发生错误。
生成的xml文件格式。
<?xml version="1.0"?>
<root>
<code>0</code>
<msg>OK</msg>
<data>
<agentid>1</agentid>
<imei>3224</imei>
<typeid>2</typeid>
<type>Truck</type>
<vehiclenumber>12121</vehiclenumber>
<folder>folder1</folder>
<created_time>1493813160</created_time>
<current_mileage>4232</current_mileage>
<status>
<active>1</active>
<speed>0</speed>
<direction>303</direction>
<lat>3.79092</lat>
<lon>69.51279</lon>
<alt>1</alt>
<satsinview>18</satsinview>
<unixtimestamp>1503389638</unixtimestamp>
<sensors>
<Ignition sensor="">Off</Ignition sensor><External power="" supply="">On</External power supply>
</sensors>
</status>
</data>
</root>
有什么建议吗?