zoukankan      html  css  js  c++  java
  • C#调用XmlSerializer序列化时生成CDATA节点解决方法

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }        
    }
    引用内容
    <?xml version="1.0"?>
    <Person xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Name>dnawo</Name>
      <Age>100</Age>
    </Person>


    例2

    public class Person
    {
        public XmlNode Name { get; set; } //XmlNodeType.CDATA
        public XmlNode Age { get; set; } //XmlNodeType.Text
    }
    引用内容
    <?xml version="1.0"?>
    <Person xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Name><![CDATA[dnawo]]></Name>
      <Age>100</Age>
    </Person>


    例1的实体类我们比较常用,赋值取值方便,但序列化时不能生成CDATA节点,例2的实体类序列化时可以生成CDATA节点,但使用不方便,于是将两个例子优点做了下结合:

    using System;
    using System.IO;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    namespace ConsoleApplication1
    {
        public partial class Person
        {
            [XmlIgnore]
            public string Name { get; set; }        

            [XmlIgnore]
            public int Age { get; set; }        
        }

        public partial class Person
        {
            [XmlElement("Name")]
            public XmlNode aaa
            {
                get
                {
                    XmlNode node = new XmlDocument().CreateNode(XmlNodeType.CDATA, "", "");
                    node.InnerText = Name;
                    return node;
                }
                set { } //省略则aaa不会被序列化
            }
            [XmlElement("Age")]
            public XmlNode bbb
            {
                get
                {
                    XmlNode node = new XmlDocument().CreateNode(XmlNodeType.Text, "", "");
                    node.InnerText = Age.ToString();
                    return node;
                }
                set { } //省略则bbb不会被序列化
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                string result = string.Empty;
                Person person = new Person() { Name = "dnawo", Age = 100 };
                using (MemoryStream output = new MemoryStream())
                {
                    XmlSerializer serializer = new XmlSerializer(person.GetType());
                    serializer.Serialize(output, person);
                    result = Encoding.UTF8.GetString(output.ToArray());
                }
                Console.WriteLine(result);

                Console.ReadKey();
            }
        }
    }
    引用内容
    <?xml version="1.0"?>
    <Person xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <Name><![CDATA[dnawo]]></Name>
      <Age>100</Age>
    </Person>


    常见问题

    问:为什么例2实体类属性类型不直接用XmlCDataSection、XmlText?
    答:用XmlCDataSection没问题,而XmlText序列化时会失败,提示反射类型“System.Xml.XmlText”出错。

    参考资料

    [1].使用 XmlSerializer 控制序列化生成 CDATA 内容:http://blog.csdn.net/hwj383/article/details/5780962 

  • 相关阅读:
    Eclipse+EPIC+PadWalker
    Commit message 和 Change log 编写指南
    把perl脚本编译成exe
    Qt使用中碰到的问题
    Python——函数 7、位置参数与默认参数之间的关系
    Python——函数 6、默认参数
    Python——函数 5、位置参数与关键字参数
    Python——函数 4、形参的应用
    Python——函数 3、实参与形参
    Python——函数 2、返回值
  • 原文地址:https://www.cnblogs.com/Alex80/p/9149344.html
Copyright © 2011-2022 走看看