
·您现在的位置: 云翼网络 >> 文章中心 >> 网站建设 >> 网站建设开发 >> ASP.NET网站开发 >> linqtoxml
今天在做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;
引入之后一切正常