zoukankan      html  css  js  c++  java
  • 【转】C# XML序列化去掉XML默认的命名空间及声明头

    http://blog.csdn.net/aoshilang2249/article/details/44860155

    重点:

    XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
        namespaces.Add(string.Empty, string.Empty);
    // 序列化这个对象
    int obj = 1;
    XmlSerializer serializer = new XmlSerializer(obj.GetType());
    
    // 将对象序列化输出到文件
    FileStream stream = new FileStream("hh.xml", FileMode.Create);
    
    XmlWriterSettings settings = new XmlWriterSettings();
    settings.Indent = true;
    settings.IndentChars = "    ";
    settings.NewLineChars = "
    ";
    settings.Encoding = Encoding.UTF8;
    //settings.OmitXmlDeclaration = true;  // 不生成声明头
    
    using (XmlWriter xmlWriter = XmlWriter.Create(stream, settings))
    {
        // 强制指定命名空间,覆盖默认的命名空间
        XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
        namespaces.Add(string.Empty, string.Empty);
        serializer.Serialize(xmlWriter, obj, namespaces);
        xmlWriter.Close();
    };
    stream.Close();
  • 相关阅读:
    sql server中count(*),count(col),count(1)的区别
    oracle 存储过程(1)
    Java线程:概念及原理
    H2 应用实例2
    H2 应用实例1
    H2 database 应用
    H2 database 操作操作内存表
    JDK 环境变量配置
    MySQL CREATE TRIGGER (1)
    MySQL 事务1
  • 原文地址:https://www.cnblogs.com/runliuv/p/7326065.html
Copyright © 2011-2022 走看看