·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> linqtoxml

linqtoxml

作者:佚名      ASP.NET网站开发编辑:admin      更新时间:2022-07-23

今天在做xml解析的时候,引入命名空间

using System. Xml. Linq;

然后

XElement news = root. Element( "newslist" );
NewsUnit[ ] units = news. Elements( "news" ). Select(
                            n => new NewsUnit
                            {
                                id = n. Element( "id" ). Value. ToInt32( ),
                                title = n. Element( "title" ). Value,
                                author = n. Element( "author" ). Value,
                                authorID = n. Element( "authorid" ). Value. ToInt32( ),
                                pubDate = n. Element( "pubDate" ). Value,
                                commentCount = n. Element( "commentCount" ). Value. ToInt32( ),
                                url = n.Element("url") == null ? null : n.Element("url").Value,
                                newsType = new NewsType
                                {
                                    type = n. Element( "newstype" ). Element( "type" ). Value. ToInt32( ),
                                    attachment = n.Element("newstype").Element("attachment") == null ? 
                                                                                    n. Element( "id" ). Value : 
                                                                                    n. Element("newstype"). Element("attachment").Value,
                                    authorID = n. Element( "newstype" ). Element( "authoruid2" ). Value. ToInt32( ),
                                },
                            }
                        ). ToArray( );

结果说没有Select方法,

原因是没有引入

using System. Linq;

引入之后一切正常