3

下面是我的 XML 文件:

<Visibilities>
    <Visibilities AppName="Test2">
      <Visibility DisplayName="Full Name">
        <FullName>
          <FirstName PropertyName="FullName.FirstName" DisplayName="First Name" ListOrder="3"></FirstName>
          <MiddleName PropertyName="FullName.MiddleName" DisplayName="Middle Name" ListOrder="2"></MiddleName>
          <LastName PropertyName="FullName.LastName" DisplayName="Last Name" ListOrder="1"></LastName>
        </FullName>
      </Visibility>
    </Visibilities>
  </Visibilities>

下面是我的课

#region FullName
    [Serializable]
    public class FullName
    {
        private String strFirstName;
        [XmlElement("FirstName")]
        public String FirstName
        {
            get { return strFirstName; }
            set { strFirstName = value; }
        }

        private String strMiddleName;
        [XmlElement("MiddleName")]
        public String MiddleName
        {
            get { return strMiddleName; }
            set { strMiddleName = value; }
        }

        private String strLastName;
        [XmlElement("LastName")]
        public String LastName
        {
            get { return strLastName; }
            set { strLastName = value; }
        }

        [XmlElement("ListOrder")]
        public int ListOrder { get; set; }
    }
    #endregion

    #region Visibility
    [Serializable]
    public class Visibility
    {
        public FullName FullName { get; set; }

        [XmlAttribute("DisplayName")]
        public String DisplayName { get; set; }

    }
    #endregion

    #region Visibilities
    [Serializable]
    public class Visibilities
    {
        [XmlAttribute("AppName")]
        public String AppName { get; set; }

        [XmlElement("Visibility")]
        public List<Visibility> Visibility { get; set; }
    }
    #endregion

下面的代码行是我的阅读方式:

foreach (Visibility vis in l.Visibility.ToList())
                {
                    Console.WriteLine(vis.DisplayName);
                    Console.WriteLine("{0}{1}", vis.FullName.FirstName, Reflect.GetPropValue<String>(cust, vis.FullName.FirstName));
                }

我必须做哪些修改才能使用类或 readind 代码部分从 First、Middle、Last Name 读取属性值,即 PropertyName、DisplayName、ListOrder?

4

1 回答 1

2

我认为您将需要另一个类来保存 3 个属性,并用属性标记这些XmlAttribute属性

[Serializable]
public class Name
{
    [XmlAttribute]
    public string PropertyName { get; set; }

     [XmlAttribute]
    public string DisplayName { get; set; }

     [XmlAttribute]
    public int ListOrder { get; set; }
}

[Serializable]
public class FullName
{
    private Name strFirstName;
    [XmlElement("FirstName")]
    public Name FirstName
    {
        get { return strFirstName; }
        set { strFirstName = value; }
    }

    private Name strMiddleName;
    [XmlElement("MiddleName")]
    public Name MiddleName
    {
        get { return strMiddleName; }
        set { strMiddleName = value; }
    }

    private Name strLastName;
    [XmlElement("LastName")]
    public Name LastName
    {
        get { return strLastName; }
        set { strLastName = value; }
    }

    [XmlElement("ListOrder")]
    public int ListOrder { get; set; }
}

[Serializable]
public class Visibility
{
    public FullName FullName { get; set; }

    [XmlAttribute("DisplayName")]
    public String DisplayName { get; set; }

}

[Serializable]
public class Visibilities
{
    [XmlAttribute("AppName")]
    public String AppName { get; set; }

    [XmlElement("Visibility")]
    public List<Visibility> Visibility { get; set; }
}

序列化:

<Visibilities>
  <Visibilities AppName="Test2">
    <Visibility DisplayName="Full Name">
      <FullName>
        <FirstName PropertyName="FullName.FirstName" DisplayName="First Name" ListOrder="3" />
        <MiddleName PropertyName="FullName.MiddleName" DisplayName="Middle Name" ListOrder="3" />
        <LastName PropertyName="FullName.LastName" DisplayName="Last Name" ListOrder="3" />
        <ListOrder>0</ListOrder>
      </FullName>
    </Visibility>
</Visibilities>

于 2013-05-19T09:15:39.573 回答