2010-09-07 55 views
0

我序列化对象与下面的代码到XML:如何摆脱序列化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> 

我怎样才能摆脱他们

+0

这听起来像是 - HTTP:// stackoverflow.com/questions/258960/how-to-serialize-an-object-to-xml-without-getting-xmlns – Vin 2010-09-07 18:26:34

+0

[XmlSerializer:删除不必要的xsi和xsd命名空间]的可能重复(http://stackoverflow.com/问题/ 760262/xmlserializer-remove-unnecessary-xsi-and-xsd-namespaces) – 2011-03-24 10:42:51

回答