日期:2014-05-20  浏览次数:20912 次

linq to xml 缺少根元素的问题
我通过新浪围脖的api
得到如下StreamReader流
XML code

<statuses>
<status>
<created_at>Thu Jun 09 08:54:42 +0800 2011</created_at>
<id>12005331530</id>
<text>哈哈哈哈!!9.5分!!谁能比我高 http://t.cn/aoopfZ</text>
<source>
<a href="http://www.jjdd.com">简简单单网</a>
</source>
<favorited>false</favorited>
<truncated>false</truncated>
<geo/>
<in_reply_to_status_id/>
<in_reply_to_user_id/>
<in_reply_to_screen_name/>
<thumbnail_pic>
http://ww1.sinaimg.cn/thumbnail/64aa640cjw1di0hp0b2xzj.jpg
</thumbnail_pic>
<bmiddle_pic>
http://ww1.sinaimg.cn/bmiddle/64aa640cjw1di0hp0b2xzj.jpg
</bmiddle_pic>
<original_pic>
http://ww1.sinaimg.cn/large/64aa640cjw1di0hp0b2xzj.jpg
</original_pic>
<mid>211110609778962</mid>
<user>
<id>1688888332</id>
<screen_name>笑nicesmile</screen_name>
<name>笑nicesmile</name>
<province>35</province>
<city>2</city>
<location>福建 厦门</location>
<description>只是个爱笑却忧郁的80后</description>
<url>http://fj.1schina.com</url>
<profile_image_url>http://tp1.sinaimg.cn/1688888332/50/1283096131/0</profile_image_url>
<domain>1schina</domain>
<gender>f</gender>
<followers_count>274</followers_count>
<friends_count>210</friends_count>
<statuses_count>1385</statuses_count>
<favourites_count>6</favourites_count>
<created_at>Tue Feb 02 00:00:00 +0800 2010</created_at>
<following>false</following>
<verified>false</verified>
<allow_all_act_msg>false</allow_all_act_msg>
<geo_enabled>true</geo_enabled>
</user>
</status>
</sratuses>


然后
C# code

                    XElement temp = XElement.Load(reader);//提示没有根元素,请问我要怎么处理?
                    var tempNode = (from tn in temp.Elements("status")
                                    select new
                                    {
                                        message = tn.Element("text"),
                                        picurl = tn.Element("original_pic")
                                    });






------解决方案--------------------
</sratuses>

=>

</satuses>


XML问题。
------解决方案--------------------
搞清楚所以然。

那个xml是sina返回的还是你手写的。

</sratuses>应该是</statuses>
或者
<statuses>应该是<sratuses>

总之,应该配对:

<a>
。。。
</a> 这个是合法的 xml

<a>
。。。
</b> 这个是非法的 xml






------解决方案--------------------
楼主发的问题,跟自己给的解法没关系,到底是什么问题,没人知道.
------解决方案--------------------
解析的xml数据没有xml文件声明,这种情况应该是不能正常解析的.
楼主找找原因
------解决方案--------------------
如果获取的数据就是这样的话,你可以先写到某个xml文件当中,之后读取,再进行操作.