当前位置: 首页>>代码示例>>C#>>正文


C# XmlAttributes.XmlDefaultValue属性代码示例

本文整理汇总了C#中System.Xml.Serialization.XmlAttributes.XmlDefaultValue属性的典型用法代码示例。如果您正苦于以下问题:C# XmlAttributes.XmlDefaultValue属性的具体用法?C# XmlAttributes.XmlDefaultValue怎么用?C# XmlAttributes.XmlDefaultValue使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在System.Xml.Serialization.XmlAttributes的用法示例。


在下文中一共展示了XmlAttributes.XmlDefaultValue属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System; 
using System.IO; 
using System.Xml; 
using System.Xml.Serialization; 
using System.ComponentModel; 

// This is the class that will be serialized. 
public class Pet 
{
   // The default value for the Animal field is "Dog". 
   [DefaultValueAttribute("Dog")] 
   public string Animal ; 
} 

public class Run 
{ 
   public static void Main() 
   { 
      Run test = new Run();
      test.SerializeObject("OverrideDefaultValue.xml");
      test.DeserializeObject("OverrideDefaultValue.xml"); 
   }
 
   // Return an XmlSerializer used for overriding. 
   public XmlSerializer CreateOverrider() 
   { 
      // Create the XmlAttributeOverrides and XmlAttributes objects. 
      XmlAttributeOverrides xOver = new XmlAttributeOverrides(); 
      XmlAttributes xAttrs = new XmlAttributes(); 

      // Add an override for the default value of the GroupName. 
      Object defaultAnimal= "Cat";
      xAttrs.XmlDefaultValue = defaultAnimal; 

      // Add all the XmlAttributes to the XmlAttributeOverrides object. 
      xOver.Add(typeof(Pet), "Animal", xAttrs); 

      // Create the XmlSerializer and return it.
      return new XmlSerializer(typeof(Pet), xOver);
   }

   public void SerializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer = CreateOverrider(); 

      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename); 

      // Create an instance of the class that will be serialized. 
      Pet myPet = new Pet(); 

      /* Set the Animal property. If you set it to the default value,
         which is "Cat" (the value assigned to the XmlDefaultValue
         of the XmlAttributes object), no value will be serialized.
         If you change the value to any other value (including "Dog"),
         the value will be serialized.
      */
      // The default value "Cat" will be assigned (nothing serialized).
      myPet.Animal= ""; 
      // Uncommenting the next line also results in the default 
      // value because Cat is the default value (not serialized).
      //  myPet.Animal = "Cat"; 
      
      // Uncomment the next line to see the value serialized:
      // myPet.Animal = "fish";
      // This will also be serialized because Dog is not the 
      // default anymore.
      //  myPet.Animal = "Dog";
      // Serialize the class, and close the TextWriter. 
      mySerializer.Serialize(writer, myPet); 
      writer.Close(); 
   } 

   public void DeserializeObject(string filename) 
   {
      XmlSerializer mySerializer = CreateOverrider();
      FileStream fs = new FileStream(filename, FileMode.Open);
      Pet myPet= (Pet)mySerializer.Deserialize(fs);
      Console.WriteLine(myPet.Animal);
   }
}
开发者ID:.NET开发者,项目名称:System.Xml.Serialization,代码行数:83,代码来源:XmlAttributes.XmlDefaultValue


注:本文中的System.Xml.Serialization.XmlAttributes.XmlDefaultValue属性示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。