如何使用XMLSerializer添加没有前缀的默认命名空间

我正在尝试使用XmlSerializer生成一个包含默认命名空间而没有前缀的XML文档,例如

     

使用以下代码……

 string xmlizedString = null; MemoryStream memoryStream = new MemoryStream(); XmlSerializer xs = new XmlSerializer(typeof(ExportMyRecord)); XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces(); xmlnsEmpty.Add(string.Empty, string.Empty); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8); xs.Serialize(xmlTextWriter, myRecord, xmlnsEmpty); memoryStream = (MemoryStream)xmlTextWriter.BaseStream; xmlizedString = this.UTF8ByteArrayToString(memoryStream.ToArray()); 

和class级结构……

 [Serializable] [XmlRoot("MyRecord")] public class ExportMyRecord { [XmlAttribute("ID")] public int ID { get; set; } 

现在,我尝试了各种选择……

 XmlSerializer xs = new XmlSerializer (typeof(ExportMyRecord),"http://www.website.com/MyRecord"); 

要么 …

 [XmlRoot(Namespace = "http://www.website.com/MyRecord", ElementName="MyRecord")] 

给我 …

     

我需要XML来拥有没有前缀的命名空间,因为它会转到第三方提供商,他们会拒绝所有其他选择。

你去:

 ExportMyRecord instance = GetInstanceToSerializeFromSomewhere(); XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces(); xmlnsEmpty.Add(string.Empty, "http://www.website.com/MyRecord"); var serializer = new XmlSerializer( instance.GetType(), "http://www.website.com/MyRecord" );