从xml文件中读取

时间:2016-05-09 11:29:54

标签: c# xml linq-to-xml

我有一个像这样的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上的文档并且没有找到我和#后,我已经陷入困境了。 39;我正在寻找我认为在这里提问可能更快更容易。

基本上,首先我尝试将XML中的UserName字段读入UserName对象的LogOnUser属性。我试过这个:

var XMLUser = XElement.Load(LogOnUserXMLFilePath);

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

但是这会将UserName设置为&#34; LogOnUsersInfo&#34;所以显然我需要深入了解xml,但我不知道如何。

有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:1)

作为初学者,你可以这样做:

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");

答案 1 :(得分:1)

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

String old_date="Thu Jul 05 22:15:04 GMT+05:30 2012";

private String Convert_Twitter_Date(String old_date) {

        SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy hh:mm:ss");
        SimpleDateFormat old = new SimpleDateFormat("EEE MMM dd HH:mm:ss ZZZZZ yyyy",Locale.ENGLISH);
        old.setLenient(true);

            Date date = null;
            try {

                date = old.parse(old_date);

            } catch (ParseException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        return sdf.format(date);    
}