【问题标题】:Difference between using XMLRoot/XMLElement and using Serializable() attributes (in c#)使用 XMLRoot/XMLElement 和使用 Serializable() 属性之间的区别(在 c# 中)
【发布时间】:2011-05-13 00:27:25
【问题描述】:

使用 XMLRoot/XMLElement 和使用 Serializable() 属性有什么区别? 我怎么知道什么时候使用每个?

【问题讨论】:

    标签: c# serialization serializable xmlroot


    【解决方案1】:

    这里的描述不够深入,但我认为这是一个很好的起点。

    XmlRootAttribute - 用于为将成为被序列化对象图的根元素的类提供模式信息。这只能应用于类、结构、枚举、返回值的接口。

    XmlElementAttribute - 为类的属性提供模式信息,控制如何将它们序列化为子元素。该属性只能应用于字段(类变量成员)、属性、参数和返回值。

    前两个 XmlRootAttributeXmlElementAttribute 与 XmlSerializer 相关。 而下一个由运行时格式化程序使用,并且在使用 XmlSerialization 时不适用。

    SerializableAtttrible - 用于表示该类型可以被 SoapFormatter 或 BinaryFormatter 等运行时格式化程序序列化。仅当您需要使用其中一种格式化程序对类型进行序列化时才需要这样做,并且可以将其应用于委托、枚举、结构和类。

    这是一个简单的例子,可能有助于澄清上述内容。

    // This is the root of the address book data graph
    // but we want root written out using camel casing
    // so we use XmlRoot to instruct the XmlSerializer
    // to use the name 'addressBook' when reading/writing
    // the XML data
    [XmlRoot("addressBook")]
    public class AddressBook
    {
      // In this case a contact will represent the owner
      // of the address book. So we deciced to instruct
      // the serializer to write the contact details out
      // as <owner>
      [XmlElement("owner")]
      public Contact Owner;
    
      // Here we apply XmlElement to an array which will
      // instruct the XmlSerializer to read/write the array
      // items as direct child elements of the addressBook
      // element. Each element will be in the form of 
      // <contact ... />
      [XmlElement("contact")]
      public Contact[] Contacts;
    }
    
    public class Contact
    {
      // Here we instruct the serializer to treat FirstName
      // as an xml element attribute rather than an element.
      // We also provide an alternate name for the attribute.
      [XmlAttribute("firstName")]
      public string FirstName;
    
      [XmlAttribute("lastName")]
      public string LastName;
    
      [XmlElement("tel1")]
      public string PhoneNumber;
    
      [XmlElement("email")]
      public string EmailAddress;
    }
    

    鉴于上述情况,使用 XmlSerializer 序列化的 AddressBook 实例将提供以下格式的 XML

    <addressBook>
      <owner firstName="Chris" lastName="Taylor">
        <tel1>555-321343</tel1>
        <email>chris@guesswhere.com</email>
      </owner>
      <contact firstName="Natasha" lastName="Taylor">
        <tel1>555-321343</tel1>
        <email>natasha@guesswhere.com</email>
      </contact>
      <contact firstName="Gideon" lastName="Becking">
        <tel1>555-123423</tel1>
        <email>gideon@guesswhere.com</email>
      </contact>
    </addressBook>
    

    【讨论】:

    • 你能告诉我们如何在地址簿标签内为所有联系人添加标签的xml添加属性吗?
    • @Dhanashree,您是否要求将联系人元素包装在父联系人元素中?如果是这样,那么您可以从“Contact[] 联系人”中删除 [XmlElement("contact")],如果您想控制名称,则可以将当前属性替换为 [XmlArray("contacts")], XmlArrayItem(" contact")] 将控制根的名称和集合中的项目。
    • 谢谢!这有帮助:)
    猜你喜欢
    • 2011-05-31
    • 2011-03-20
    • 2011-01-22
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2020-09-01
    • 2010-10-31
    相关资源
    最近更新 更多