how to read xml attribute using C# classes using deserialization

℡╲_俬逩灬. 提交于 2020-02-22 05:13:31

问题


Below is My XML File :

<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>

Below are my classes

#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

Below Lines of codes is how I am reading :

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

What Modifications I have to do so that I can read Attribute values i.e PropertyName,DisplayName,ListOrder from First,Middle,Last Name using classes or the readind code part?


回答1:


I think you are going to need another class to hold the 3 properties, and mark those properties with the XmlAttribute attribute

[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; }
}

Serialized:

<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>



来源:https://stackoverflow.com/questions/16632409/how-to-read-xml-attribute-using-c-sharp-classes-using-deserialization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!