当前位置: 动力学知识库 > 问答 > 编程问答 >

.NET XmlSerializer XML format

问题描述:

Is there a way to make XmlSerializer to serialize object' properties as attributes

instead of as elements ?

like:

Person.Phone="1 800 CALL ME"

would become:

[Person Phone="1 800 CALL ME" /]

instead of:

[Person][Phone]1 800 CALL ME[/Phone][/Person]

网友答案:

Sure...

public class Person
{
    [XmlAttribute]
    public string Phone {get;set;}
}

You can also change the name/namespace etc by looking at the attribute parameters.

网友答案:

You can do this way

[Serializable]
public class Person
{         
      [XmlAttribute()]
      public string Phone { get; set;}

      Or

      [XmlAttribute("Phone")]
      public string Phone123 { get;set;}
}
网友答案:

Use the XmlAttribute attribute

分享给朋友:
您可能感兴趣的文章:
随机阅读: