linq to xml

今天在做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;

引入之后一切正常

原文地址:https://www.cnblogs.com/wanghongxu/p/4224095.html