首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

关于LiINQ筛选和排序XML元素的有关问题-未将对象引用设置到对象的实例

2013-02-24 
关于LiINQ筛选和排序XML元素的问题-未将对象引用设置到对象的实例。本帖最后由 JCY1009015337 于 2013-02-1

关于LiINQ筛选和排序XML元素的问题-未将对象引用设置到对象的实例。
本帖最后由 JCY1009015337 于 2013-02-18 15:23:48 编辑 代码: 
static XElement CreateUserList1()
        {
            XElement man1 = new XElement("Man",
                new XAttribute("ID", "001"),
                new XAttribute("Age", "22"),
                new XElement("Name", "李四"),
                new XElement("XingBie", "男"), 
                new XElement("Phone", "131123456789"), 
                new XElement("Emial", "LiSi@126.com"));
            XElement man2 = new XElement("Man",
               new XAttribute("ID", "002"),
               new XAttribute("Age", "23"),
               new XElement("Name", "张三"),
               new XElement("XingBie", "男"),
               new XElement("Phone", "132333456789"),
               new XElement("Emial", "Zhangsan@126.com"));
            XElement woman1 = new XElement("Woman",
               new XAttribute("ID", "003"),
               new XAttribute("Age", "19"),
               new XElement("Name", "黄花"),
               new XElement("XingBie", "女"),
               new XElement("Phone", "130003456789"),
               new XElement("Emial", "huanghua@126.com"));
            XElement userList = new XElement("UserList",new XAttribute("Count","3"),man1,woman1,man2);
            return userList;
        }
 static void QueryComplexElement()//筛选和排序XML元素
        {
            XElement userList = CreateUserList1();
            var oldUsers = from user in userList.Elements() where int.Parse  (user.Attribute("Age").Value) > 20 orderby user.Attribute("Age").Value descending select user;


            foreach (var item in oldUsers)
                Console.WriteLine(item);
            var anOldUsers = from user in userList.Elements()
                             where int.Parse(user.Attribute("Age").Value) > 20
                             orderby user.Attribute("Age").Value
                             select new { Name = user.Element("Name").Value,
                                 Age = user.Attribute("Age").Value,
                                 Email = user.Element("Email").Value };
            Console.WriteLine();
            foreach (var item in anOldUsers)
            {
                Console.WriteLine(item);
            }
        }
为什么运行函数QueryComplexElement()后有“未将对象引用设置到对象的实例”的错误?
关于LiINQ筛选和排序XML元素的有关问题-未将对象引用设置到对象的实例
[解决办法]
 new XElement("Emial", "Zhangsan@126.com"));

 Email = user.Element("Email").Value
[解决办法]
这代码写的问题多多啊:

void Main()
{
  XElement userList = CreateUserList1();
  
 var anOldUsers = from user in userList.Descendants("Man")
  where user.Attribute("Age") !=null && int.Parse(user.Attribute("Age").Value) > 20
  orderby user.Attribute("Age").Value
  select new { Name = user.Element("Name").Value,
  Age = user.Attribute("Age").Value,
  Email = user.Element("Email").Value };
 
 foreach (var item in anOldUsers)
 {
 Console.WriteLine(item);
 }
}
static XElement CreateUserList1()


 {
 XElement man1 = new XElement("Man",
 new XAttribute("ID", "001"),
 new XAttribute("Age", "22"),
 new XElement("Name", "李四"),
 new XElement("XingBie", "男"), 
 new XElement("Phone", "131123456789"), 
 new XElement("Email", "LiSi@126.com"));
 XElement man2 = new XElement("Man",
new XAttribute("ID", "002"),
new XAttribute("Age", "23"),
new XElement("Name", "张三"),
new XElement("XingBie", "男"),
new XElement("Phone", "132333456789"),
new XElement("Email", "Zhangsan@126.com"));
 XElement woman1 = new XElement("Woman",
new XAttribute("ID", "003"),
new XAttribute("Age", "19"),
new XElement("Name", "黄花"),
new XElement("XingBie", "女"),
new XElement("Phone", "130003456789"),
new XElement("Email", "huanghua@126.com"));
 XElement userList = new XElement("UserList",new XAttribute("Count","3"),man1,woman1,man2);
 return userList;
 }

热点排行