【问题标题】:How can I get rid of unwanted attributes in serialized XML?如何摆脱序列化 XML 中不需要的属性?
【发布时间】:2010-09-07 18:16:56
【问题描述】:

我正在使用以下代码将对象序列化为 XML:

    public static string SerializeToString<T>(T objectToBeSerialized, string defaultNamespace)
    {
        StringBuilder stringBuilder = new StringBuilder();
        XmlWriterSettings xmlSettings = new XmlWriterSettings()
        {
            CloseOutput = true,
            Indent = true,
            OmitXmlDeclaration = true
        };

        using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, xmlSettings))
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T), defaultNamespace);
            serializer.Serialize(xmlWriter, objectToBeSerialized);

            return stringBuilder.ToString();
        }
    }

我已经在设置默认命名空间(“http://schemas.somecompany.com/online/someservice/sync/2008/11”);但是,我的输出仍然包含默认的“xmlns:xsi”和“xmlns:xsd

<RootTag ***xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"*** xmlns="http://schemas.somecompany.com/online/someservice/sync/2008/11">
  <SomeTag>
    <More>false</More>
  </SomeTag>
</RootTage>

我怎样才能摆脱它们?

【问题讨论】:

标签: c# .net xml-serialization


【解决方案1】:

【讨论】:

猜你喜欢
  • 2016-07-20
  • 2011-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-23
  • 2017-04-03
相关资源
最近更新 更多