如何获取XDocument元素值

时间:2014-02-16 06:43:12

标签: c# xml linq linq-to-xml

我一直在为Minecraft Launcher编码。我使用以下代码将用户详细信息保存到XML文件:

public static void SaveUserDetails(string username, string clientToken, 
  string accessToken, string UUID, int lengthOfPass)
{
    XDocument user = new XDocument(
        new XDeclaration("1.0", "utf-8", "yes"),
        new XComment(String.Format("User details -- {0}", username)),
        new XElement("user",
            new XElement("username", username),
            new XElement("clientToken", clientToken),
            new XElement("accessToken", accessToken),
            new XElement("userUUID", UUID),
            new XElement("lengthOfPass", lengthOfPass)));
    SaveXDoc(user, Path.Combine(Variables.usersFolder, username + ".user"));
}

最终产品如下所示:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--User details - - Ilan321-->
<user>
  <username>Ilan321</username>
  <clientToken>clienttoken</clientToken>
  <accessToken>accesstoken</accessToken>
  <userUUID>uuid</userUUID>
  <lengthOfPass>5</lengthOfPass>
</user>

现在,我想读取元素中的值。我这样做是为了这个代码:

string details = null;
XDocument doc = XDocument.Load(userFile);
var data = from item in doc.Descendants("user")
           select new
           {
               user = item.Element("username").Value,
               clientToken = item.Element("clientToken").Value,
               accessToken = item.Element("accessToken").Value,
               userUUID = item.Element("userUUID").Value,
               passLength = item.Element("lengthOfPass").Value
           };
foreach (var thing in data)
    details = thing.ToString(); // there is only one string

然而,我觉得这非常不专业(关于遍历var data的foreach)。有没有办法以字符串格式快速获取所有值而不是使用foreach?我尝试将data转换为字符串,但它只是将其转换为类型,而不是内容。

3 个答案:

答案 0 :(得分:2)

您可以执行以下操作

foreach (var thing in data)
{
    details  = string.Format("user :{0}, clientToken: {1}",thing.user,thing.clientToken);
}

或者您最好为用户创建新课程,然后您可以overide the ToString method

public class User
{
    public string User  { get; set; }
    public string ClientToken { get; set; }
    public string AccessToken { get; set; }
    public string UserUUID { get; set; }
    public string PassLength { get; set; }


    public override string ToString()
    {
        return string.Format("user :{0}, clientToken: {1}, AccessToken: {2},UserUUID :{3},  PassLength:{4}", User, ClientToken, AccessToken, UserUUID, PassLength);
    }

}

然后

var data = from item in doc.Descendants("user")
            select new User
            {
                User = item.Element("username").Value,
                ClientToken = item.Element("clientToken").Value,
                AccessToken = item.Element("accessToken").Value,
                UserUUID = item.Element("userUUID").Value,
                PassLength = item.Element("lengthOfPass").Value
            };
details = data.FirstOrDefault().ToString();

答案 1 :(得分:1)

如果您希望只有一个节点使用First来获取唯一元素:

 details = data.First().ToString();

如果元素可能不存在,请使用FirstOrDefault

答案 2 :(得分:1)

你可以这样做:

    var item = doc.Element("user");

    var data = new
    {
        user = item.Element("username").Value,
        clientToken = item.Element("clientToken").Value,
        accessToken = item.Element("accessToken").Value,
        userUUID = item.Element("userUUID").Value,
        passLength = item.Element("lengthOfPass").Value
    };

    details = data.ToString();

我得到的代码与您的代码完全相同。