我正在使用visual studio 2012 asp.net c#。我无法读取XML字符串变量" j"。请帮忙。我想读取字符串变量" j"并在标签上显示XML值。请告诉我正确的密码。谢谢
WeatherAPI(lbldistrict.Text + "," + "India");
XmlDocument j = WeatherAPI(lbldistrict.Text + "," + "India");
var myXml = j.DocumentElement.InnerXml.ToString();
XmlDocument doc = new XmlDocument();
doc.LoadXml(myXml); //XML Error: There are multiple root elements
XmlTextReader reader = new XmlTextReader("j");
XmlNodeType type;
while (reader.Read())
{
type = reader.NodeType;
if (type == XmlNodeType.Element)
{
if (reader.Name == "temp_C")
{
reader.Read();
lbltemp.Text = reader.Value;
}
if (reader.Name == "windspeedKmph")
{
reader.Read();
lblwind.Text = reader.Value;
}
if (reader.Name == "weatherIconUrl")
{
reader.Read();
ImgWeather.ImageUrl = reader.Value;
}
if (reader.Name == "weatherDesc")
{
reader.Read();
lblDec.Text = reader.Value;
} if (reader.Name == "tempMinC")
{
reader.Read();
lblLow.Text = reader.Value;
} if (reader.Name == "tempMaxC")
{
reader.Read();
lblHigh.Text = reader.Value;
} if (reader.Name == "humidity")
{
reader.Read();
lblHumidity.Text = reader.Value;
}
}
}
我从Web服务获取XML文件。 XML文件看起来像:
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>
<data>
<request>
<type>City</type>
<query>Madhubani, India</query>
</request>
<current_condition>
<observation_time>05:56 AM</observation_time>
<temp_C>30</temp_C>
<temp_F>87</temp_F>
<weatherCode>353</weatherCode>
<weatherIconUrl>
<![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png]]>
</weatherIconUrl>
<weatherDesc><![CDATA[Light rain shower]]></weatherDesc>
<windspeedMiles>8</windspeedMiles>
<windspeedKmph>13</windspeedKmph>
<winddirDegree>110</winddirDegree>
<winddir16Point>ESE</winddir16Point>
<precipMM>0.2</precipMM>
<humidity>84</humidity>
<visibility>10</visibility>
<pressure>1005</pressure>
<cloudcover>70</cloudcover>
</current_condition>
<weather>
<date>2014-09-20</date>
<tempMaxC>33</tempMaxC>
<tempMaxF>91</tempMaxF>
<tempMinC>25</tempMinC>
<tempMinF>77</tempMinF>
<windspeedMiles>9</windspeedMiles>
<windspeedKmph>15</windspeedKmph>
<winddirection>ESE</winddirection>
<winddir16Point>ESE</winddir16Point>
<winddirDegree>124</winddirDegree>
<weatherCode>353</weatherCode>
<weatherIconUrl><![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0009_light_rain_showers.png]]>
</weatherIconUrl>
<weatherDesc><![CDATA[Light rain shower]]></weatherDesc>
<precipMM>3.6</precipMM>
</weather></data>"
等
答案 0 :(得分:0)
InnerXml
本身不包含DocumentElement
本身,因此您的XML文档树中有多个根,因此,它不是XML文献。这可能有效:
var myXml = j.DocumentElement.InnerXml.ParentNode.ToString();
或者您可以将生成的XML视为一组元素节点。