如何添加默认命名空间使用的XMLSerializer没有preFIX [英] How do I add a default namespace with no prefix using XMLSerializer
本文介绍了如何添加默认命名空间使用的XMLSerializer没有preFIX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想生成使用包含默认命名空间没有preFIX 的XmlSerializer
,例如XML文档
I am trying to generate an XML document that contains the default namespace without a prefix using XmlSerializer
, e.g.
<?xml version="1.0" encoding="utf-8" ?>
<MyRecord ID="9266" xmlns="http://www.website.com/MyRecord">
<List>
<SpecificItem>
使用以下code ...
Using the following code ...
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());
和阶级结构...
[Serializable]
[XmlRoot("MyRecord")]
public class ExportMyRecord
{
[XmlAttribute("ID")]
public int ID { get; set; }
现在,我已经试过各种选项...
Now, I've tried various options ...
XmlSerializer xs = new XmlSerializer
(typeof(ExportMyRecord),"http://www.website.com/MyRecord");
或...
[XmlRoot(Namespace = "http://www.website.com/MyRecord", ElementName="MyRecord")]
给我...
<?xml version="1.0" encoding="utf-8"?>
<q1:MylRecord ID="9266" xmlns:q1="http://www.website.com/MyRecord">
<q1:List>
<q1:SpecificItem>
我需要的XML有没有preFIX命名空间,因为它是去一个第三方供应商,他们拒绝一切其他办法。
I need the XML to have the namespace without the prefix as it's going to a third party provider and they reject all other alternatives.
推荐答案
有你去:
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"
);
这篇关于如何添加默认命名空间使用的XMLSerializer没有preFIX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文