如何使用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),"https://www.website.com/MyRecord");
要么 …
[XmlRoot(Namespace = "https://www.website.com/MyRecord", ElementName="MyRecord")]
给我 …
我需要XML来拥有没有前缀的命名空间,因为它会转到第三方提供商,他们会拒绝所有其他选择。
你去:
上述就是C#学习教程:如何使用XMLSerializer添加没有前缀的默认命名空间分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
ExportMyRecord instance = GetInstanceToSerializeFromSomewhere(); XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces(); xmlnsEmpty.Add(string.Empty, "https://www.website.com/MyRecord"); var serializer = new XmlSerializer( instance.GetType(), "https://www.website.com/MyRecord" );
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1002664.html