简体   繁体   中英

XML Error: There are multiple root elements ,how to read XML string variable in asp.net c#?

I am using visual studio 2012 asp.net c#. I am unable to read XML string variable "j". Please help. I want to read the string variable "j" and display the XML values on labels. Please let me know the proper codes. Thank you

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;
        }
    }
}

I am getting XML file from web service. XML file looks like:

   "<?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>"

etc

That InnerXml does not include the DocumentElement itself so it looks like you've got multiple roots in your XML document tree, and as a consequence, it's not an XML document. This might work:

var myXml = j.DocumentElement.InnerXml.ParentNode.ToString();

Or you could just treat the resulting XML as a set of element nodes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM