zoukankan      html  css  js  c++  java
  • how to override serialization attributes at runtime

    At runtime, you can override serialization attributes. Here is an example:

        
    class Program
        {
            
    static void Main(string[] args)
            {
                XmlSerializer ser 
    = new XmlSerializer(typeof(Foo));
                Foo foo1 
    = (Foo)ser.Deserialize(XmlReader.Create(@"..\..\XMLFile1.xml"));
                Console.WriteLine(foo1.Bar);

                XmlRootAttribute newRoot 
    = new XmlRootAttribute();
                newRoot.ElementName 
    = "Root";
                newRoot.Namespace 
    = "http://example.org/";

                XmlAttributes myAttributes 
    = new XmlAttributes();
                myAttributes.XmlRoot 
    = newRoot;

                XmlAttributeOverrides myOverrides 
    = new XmlAttributeOverrides();
                myOverrides.Add(
    typeof(Foo), myAttributes);

                ser 
    = new XmlSerializer(typeof(Foo), myOverrides);
                Foo foo2 
    = (Foo)ser.Deserialize(XmlReader.Create(@"..\..\XMLFile2.xml"));
                Console.WriteLine(foo2.Bar);

            }
        }

        [XmlRoot(ElementName
    ="Root", Namespace="http://example.com/")]
        
    public class Foo
        {
            
    public string Bar { getset; }
        }

    The first deserialization consumes

    <?xml version="1.0" encoding="utf-8" ?>
    <Root xmlns="http://example.com/">
      
    <Bar>Baz</Bar>
    </Root>

    the second

    <?xml version="1.0" encoding="utf-8" ?>
    <Root xmlns="http://example.org/">
      
    <Bar>Baz</Bar>
    </Root>


  • 相关阅读:
    IndexedDB demo showcase
    javascript for
    IndexedDB
    web sql Database
    webSql
    哈哈 代表月亮
    网易
    Android 百度地图 SDK v3.0.0 (一)
    iOS使用Instrument的Leaks查找代码内存泄露
    换主页轮播的主题图片(4、删除)---轻开电子商务系统(企业入门级B2C站点)
  • 原文地址:https://www.cnblogs.com/sskset/p/1743221.html
Copyright © 2011-2022 走看看