简体   繁体   English

从xml文件读取

[英]Reading from an xml file

I have an xml file like this: 我有一个像这样的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>

And I have a class with properties that match the values in said xml file: 我有一个类,其属性与所述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
}

I've tried using XDocument to read certain values from the xml file into instances of my LogOnUser class, however I've become a little stuck and after trawling through the documentation on MSDN and not finding what I'm looking for I figured it might be quicker and easier to ask a question on here. 我尝试使用XDocument将xml文件中的某些值读取到LogOnUser类的实例中,但是,我变得有点困惑,并且在MSDN上浏览了文档之后却找不到我想要的东西,我认为这可能更快,更容易在这里提出问题。

Basically to begin with I've just tried to read the UserName field from the XML into the UserName property of a LogOnUser object. 基本上,首先,我只是尝试将XML中的UserName字段读取到LogOnUser对象的UserName属性中。 I tried this: 我尝试了这个:

var XMLUser = XElement.Load(LogOnUserXMLFilePath);

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

however this sets the UserName to "LogOnUsersInfo" so obviously I need to go deeper into the xml but I don't know how. 但是,这会将UserName设置为“ LogOnUsersInfo”,因此显然我需要更深入地研究xml,但是我不知道如何。

Can anyone point me in the right direction? 谁能指出我正确的方向?

As a starter, you can do as follow : 首先,您可以执行以下操作:

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

Basically, you can use Descendants() to go down the XML tree an arbitrary level deep, and use Attribute() to access XML attribute. 基本上,您可以使用Descendants()将XML树深入任意层次,并使用Attribute()来访问XML属性。

Then rights information can be obtained as follow, for example : 然后可以按以下方式获取权限信息,例如:

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

You can do an XPath search. 您可以进行XPath搜索。 You can get all the user info. 您可以获取所有用户信息。

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