如何摆脱序列化XML中不需要的属性?

时间:2010-09-07 18:16:57

标签: c# .net xml-serialization

我使用以下代码将对象序列化为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>

我怎样摆脱它们?

1 个答案:

答案 0 :(得分:0)