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 日)得到了投票,这促使我查看并批评我自己的工作。然后我意识到这完全是废话......
正如 Aaron Anodide 在 OP 问题中评论的那样,Xml 架构全错了,这就是使这项任务如此艰巨的原因......
为启用糟糕的代码而感到羞耻。
为发布糟糕的代码而感到羞耻
它应该实际实施的方式。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];
}
}
}