在C#中如何在LINQ to xml之后生成对象数组 [英] How to generate array of objects after LINQ to xml in C#

查看:159
本文介绍了在C#中如何在LINQ to xml之后生成对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的对象:

public class ClientCredentials
{
    public string UserName { get; set; }

    public string Password { get; set; }

    public string Rights { get; set; }
}

和如下所示的xml:

<?xml version="1.0" encoding="utf-8" ?> 
<users>
  <user>
      <username>playerone</username>
      <password>654321</password>
      <rights>true</rights>
  </user>
  <user>
      <username>amoreroma</username>
      <password>123456789</password>
      <rights>false</rights>
  </user>
</users>

我只想在LINQ之后生成给定XML的 ClientCredentials列表对象

我尝试过这样:

XDocument document = XDocument.Load(@"path\to\file\file.xml");
    var query = document.Descendants("users").Select(s => new ClientCredentials
                    {
                        UserName = s.Element("username").Value,
                        Password = s.Element("password").Value,
                        Rights = s.Element("rights").Value
                    }).ToList();

但出现错误参考未设置为对象的实例.

解决方案

您需要Descendants("user")而不是Descendants("users").您的usernamepasswordrights元素是user元素的子元素.不是Users元素,这就是为什么要获取NullReferenceException.的原因,也可以使用显式强制转换来避免NullReferenceException.如果找不到任何元素,由于直接访问Value属性,您的代码仍会引发异常

var query = document.Descendants("user").Select(s => new ClientCredentials
                {
                    UserName = (string)s.Element("username"),
                    Password = (string)s.Element("password"),
                    Rights = (string)s.Element("rights")
                }).ToList();

I have an object like this:

public class ClientCredentials
{
    public string UserName { get; set; }

    public string Password { get; set; }

    public string Rights { get; set; }
}

and an xml looking like this:

<?xml version="1.0" encoding="utf-8" ?> 
<users>
  <user>
      <username>playerone</username>
      <password>654321</password>
      <rights>true</rights>
  </user>
  <user>
      <username>amoreroma</username>
      <password>123456789</password>
      <rights>false</rights>
  </user>
</users>

I just want to generate a List of ClientCredentials objects after LINQ to the given XML

I tried like this:

XDocument document = XDocument.Load(@"path\to\file\file.xml");
    var query = document.Descendants("users").Select(s => new ClientCredentials
                    {
                        UserName = s.Element("username").Value,
                        Password = s.Element("password").Value,
                        Rights = s.Element("rights").Value
                    }).ToList();

but I get the error Reference not set to an instance of an object.

解决方案

You need Descendants("user") instead of Descendants("users"). Your username,password and rights elements are child element of your user elements. not Users element.That's why you are getting NullReferenceException.Also you can use the explicit cast to avoid NullReferenceException.If any element can't be found your code will still throw exception because you are accessing Value property directly.

var query = document.Descendants("user").Select(s => new ClientCredentials
                {
                    UserName = (string)s.Element("username"),
                    Password = (string)s.Element("password"),
                    Rights = (string)s.Element("rights")
                }).ToList();

这篇关于在C#中如何在LINQ to xml之后生成对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆