繁体   English   中英

从xml文件读取

[英]Reading from an xml file

我有一个像这样的xml文件:

<?xml version="1.0" encoding="utf-8" ?>
<LogOnUsersInfo>
  <GeneralInformation>
    <NumberOfUsers>2</NumberOfUsers>
    <LastUser UserName="User1"/>
  </GeneralInformation>
  <LogOnUserCollection>
    <LogOnUser UserName="User1" Password="password">
      <Rights>
        <CanCreateProducts value="true"/>
        <CanEditProducts value="true"/>
        <CanDeleteProducts value="true"/>
        <CanCreateLogIns value="true"/>
        <CanDeleteLogIns value="true"/>
        <CanBeDeleted value="true"/>
        <HasDebugRights value="false"/>
      </Rights>
    </LogOnUser>  
    <LogOnUser UserName="User2" Password="password">
      <Rights>
        <CanCreateProducts value="true"/>
        <CanEditProducts value="true"/>
        <CanDeleteProducts value="true"/>
        <CanCreateLogIns value="true"/>
        <CanDeleteLogIns value="true"/>
        <CanBeDeleted value="true"/>
        <HasDebugRights value="false"/>
      </Rights>
    </LogOnUser>
  </LogOnUserCollection>
</LogOnUsersInfo>

我有一个类,其属性与所述xml文件中的值匹配:

public class LogOnUser
{
    #region NestedTypes
    public class Rights
    {
        public bool CanCreateProducts { get; set; }
        public bool CanEditProducts { get; set; }
        public bool CanDeleteProducts { get; set; }
        public bool CanCreateLogIns { get; set; }
        public bool CanDeleteLogIns { get; set; }

        public bool CanBeDeleted { get; set; }
        public bool HasDebugRights { get; set; }
    };
    #endregion

    #region Members
    string _UserName;
    string _Password;
    bool _LoggedIn;
    Rights _UserRights;

    #endregion

    #region Construction
    public LogOnUser() { }

    public LogOnUser(string username)
    {
        _UserName = username;
    }

    public LogOnUser(string username, string password, bool cancreateproducts, bool caneditproducts, bool candeleteproducts, bool cancreatelogins, bool candeletelogins)
    {
        _UserName = username;
        _Password = password;

        _UserRights = new Rights();

        _UserRights.CanCreateProducts = cancreateproducts;
        _UserRights.CanEditProducts = caneditproducts;
        _UserRights.CanDeleteProducts = candeleteproducts;
        _UserRights.CanCreateLogIns = cancreatelogins;
        _UserRights.CanDeleteLogIns = candeletelogins;
    }
    #endregion

    #region Properties
    public string Username
    {
        get { return _UserName; }
        set { _UserName = value; }
    }

    public string Password
    {
        get { return _Password; }
        set { _Password = value; }
    }

    public bool LoggedIn
    {
        get { return _LoggedIn; }
        set { _LoggedIn = value; }
    }

    public Rights UserRights
    {
        get { return _UserRights; }
        set { _UserRights = value; }
    }
    #endregion
}

我尝试使用XDocument将xml文件中的某些值读取到LogOnUser类的实例中,但是,我变得有点困惑,并且在MSDN上浏览了文档之后却找不到我想要的东西,我认为这可能更快,更容易在这里提出问题。

基本上,首先,我只是尝试将XML中的UserName字段读取到LogOnUser对象的UserName属性中。 我尝试了这个:

var XMLUser = XElement.Load(LogOnUserXMLFilePath);

foreach(XElement e in XMLUser.DescendantsAndSelf())
{
    var user = new Model.LogOnUsers.LogOnUser(e.Name.ToString());
}

但是,这会将UserName设置为“ LogOnUsersInfo”,因此显然我需要更深入地研究xml,但是我不知道如何。

谁能指出我正确的方向?

首先,您可以执行以下操作:

foreach(XElement e in XMLUser.Descendants("LogOnUser"))
{
    var user = new Model.LogOnUsers.LogOnUser((string)e.Attribute("UserName"));
}

基本上,您可以使用Descendants()将XML树深入任意层次,并使用Attribute()来访问XML属性。

然后可以按以下方式获取权限信息,例如:

var cancreateproducts = (bool)e.Element("Rights")
                               .Element("CanCreateProducts")
                               .Attribute("value");

您可以进行XPath搜索。 您可以获取所有用户信息。

var nodes = doc.SelectNodes("//LogOnUser");
foreach(var node in nodes)
{
    var username = node.Attributes["Username"];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM