代码如下:
StringBuilder builder = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
using (XmlWriter xmlWriter = XmlWriter.Create(builder, settings))
{
XmlSerializer s = new XmlSerializer(objectToSerialize.GetType());
s.Serialize(xmlWriter, objectToSerialize);
}
生成的序列化文档包含名称空间,如下所示:
<message xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"
xmlns="urn:something">
...
</message>
要删除 xsi 和 xsd 名称空间,可以按照 如何在不获得 xmlns = “ ...”的情况下将对象序列化为 XML?给出的答案。
我希望我的消息标记为 <message>
(没有任何名称空间属性)?