3

我有以下 XML

 <?xml version="1.0" encoding="utf-8"?>
 <ErrorServer>
   <ClientIP>
     <AllowAll>false</AllowAll>
     <Client_127_0_0_1>true</Client_127_0_0_1>
   </ClientIP>
   <Users>
     <Admin>
       <Password>passw0r!d</Password>
       <NextError>83</NextError>
       <Active>true</Active>
     </Admin>
     <JimBob>
       <Password>passw0r!d</Password>
       <NextError>83</NextError>
       <Active>true</Active>
     </JimBob>
   </Users>
 </ErrorServer>

在 c# 中使用 linq 我正在尝试使用以下代码获取所有用户名(上面示例中的 Admin 和 JimBob)

    List<string> Result = new List<string>();

    XDocument xdoc = XDocument.Load("ErrorServerConfig.xml");

    //Run query
    var lv1s = from lv1 in xdoc.Descendants("ErrorServer")
               select new
               {
                   Children = lv1.Elements("Users")
               };

    //Loop through results
    foreach (var lv1 in lv1s)
    {
        foreach (var lv2 in lv1.Children)
            Result.Add(lv2.Name.ToString());
    }

    return (Result);

这不起作用,因为它只在结果中返回“用户”。

我是 linq 的新手,有人可以告诉我正确的做法吗?

4

4 回答 4

5
var result = xdoc.Descendants("Users")
                 .First()
                 .Elements()
                 .Select(e=>e.Name);
于 2013-08-24T10:28:52.257 回答
4

谢谢大家,我已经按照 Aaron Anodide 的建议更改了我的 xml,这本来就是应该的(我的错)。

XML 现在看起来像:

<ErrorServer>
  <Users>
    <User>
      <Username>Admin</Username>
      <Password>passw0r!d</Password>
      <NextError>83</NextError>
      <Active>true</Active>
    </User>
  </Users>
</ErrorServer>

并使用以下代码

1 选择所有用户名

        XDocument xdoc = XDocument.Load("ErrorServerConfig.xml");

        //Run query
        var result = from e in xdoc.Descendants("Users").Elements()
             select (string)e.Element("Username");

        //Loop through results
        foreach (string user in result)
        {
            Result += String.Format("{0}\r\n", user);
        }

2 获取密码

    XDocument xdoc = XDocument.Load("ErrorServerConfig.xml");

    //Run query
    var result = (from e in xdoc.Descendants("Users").Elements()
                 where (string)e.Element("Username") == userName
                 select e).Descendants("Password").First().Value;

两者都比我以前的 xml 代码优雅得多,感谢您的帮助和指点。

于 2013-08-24T16:14:40.467 回答
2
var res =  XDocument.Load("yourpath")
    .Descendants("Users").Elements()
    .Select(xe => xe.Name.LocalName);

如果你想返回一个IEnumerable<XName>then 使用.Name,如果你想 reutrn IEnumerable<string>use Name.LocalName。这只是我的意见,但在您的课堂上,我会将 xdoc 设置为属性。

新更新


这实际上在今天(2014 年 8 月 7 日)得到了投票,这促使我查看并批评我自己的工作。然后我意识到这完全是废话......

  1. 正如 Aaron Anodide 在 OP 问题中评论的那样,Xml 架构全错了,这就是使这项任务如此艰巨的原因......

  2. 为启用糟糕的代码而感到羞耻。

  3. 为发布糟糕的代码而感到羞耻

它应该实际实施的方式。VVVVV


XML:

<?xml version="1.0" encoding="utf-8" ?>
<ErrorServer>
  <ClientIP>
    <AllowAll>false</AllowAll>
    <Address>127.0.0.1</Address>
  </ClientIP>
  <Users>
    <User>
      <Username>Admin</Username>
      <Password>passw0r!d</Password>
      <NextError>83</NextError>
      <Active>true</Active>
    </User>
    <User>
      <Username>JimBob</Username>
      <Password>passw0r!d</Password>
      <NextError>83</NextError>
      <Active>true</Active>
    </User>
  </Users>
</ErrorServer>

课程:

#region Referencing

using System;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

#endregion

namespace Stack
{
    public class Program
    {
        public Program()
        {
            ErrorServer = ErrorServer.Deserialize( "path" );
        }

        public ErrorServer ErrorServer { get; set; }

        // This way you dont actually have to deal with LINQ and XML.
        // It's just as easy to create a few classes to hold your data, so you can use xml serialization.
        public User GetUserInfoByName( string name )
        {
            return
                ErrorServer.Users.FirstOrDefault(
                    user => user.Username.Equals( name, StringComparison.CurrentCultureIgnoreCase ) );
        }
    }

    [Serializable]
    public class ErrorServer
    {
        public ClientIP ClientIP { get; set; }

        [XmlArrayItem( "User" )]
        public User[] Users { get; set; }

        public static ErrorServer Deserialize( string path )
        {
            using (var stream = new FileStream( path, FileMode.Open ))
                return new XmlSerializer( typeof (ErrorServer) ).Deserialize( stream ) as ErrorServer;
        }
    }

    [Serializable]
    public class ClientIP
    {
        public bool AllowAll { get; set; }

        public string Address { get; set; }
    }

    [Serializable]
    public class User
    {
        public string Username { get; set; }

        public string Password { get; set; }

        public double NextError { get; set; }

        public bool Active { get; set; }
    }
}

所以看在上帝的份上,请不要使用任何低于线的东西。


更新

抱歉花了这么长时间。这是我为你准备的一个小班。

using System;
using System.Linq;
using System.Xml.Linq;

namespace StackTesting
{
    class Program
    {
        public class User
        {
         public string Username { get; set; }
         public string Pass { get; set; }
         public double Error { get; set; }
         public bool Active { get; set; }

          public User() { }
        }

        Public XDocument xDoc { get; set; }

        static void Main(string[] args)
        {
         xDoc = XDocument.Load(@"C:\Users\Trae\Documents\visual studio 2012\Projects\StackTesting\StackTesting\XMLFile1.xml");
          var user = (User) GetUserInfo("Admin");
        }

        public static User GetUserInfo(string UserName)
        {
          return xDoc.Root.Elements("Users").Elements()
            .Where(xe => xe.Element(XName.Get("Username")).Value == UserName)
            .Select(xe =>
              new User
              {
                Username = xe.Element(XName.Get("Username")).Value,
                Pass = xe.Element(XName.Get("Password")).Value,
                Error = double.Parse(xe.Element(XName.Get("NextError")).Value),
                Active = bool.Parse(xe.Element(XName.Get("Active")).Value)
              }).ToArray()[0];
        }
    }
}
于 2013-08-24T15:41:45.797 回答
1
var result = xdoc.Root
                 .Element("Users")
                 .Elements()
                 .Select(x => x.Name);
于 2013-08-24T10:33:13.610 回答