加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

.net – 如何将对象序列化为XML,而不获取xmlns =“…”?

发布时间:2020-12-16 02:02:03 所属栏目:百科 来源:网络整理
导读:有没有一种方法让我在.NET中序列化一个对象,而XML命名空间也不会自动序列化?看来默认.NET认为XSI和XSD命名空间应该包括,但我不想要他们在那里。
有没有一种方法让我在.NET中序列化一个对象,而XML命名空间也不会自动序列化?看来默认.NET认为XSI和XSD命名空间应该包括,但我不想要他们在那里。
啊…永远不会。总是在问题提出后的搜索产生答案。我正在序列化的对象是obj,已经被定义。将一个具有单个空命名空间的XMLSerializerNamespace添加到集合中。

在VB中这样:

Dim xs As New XmlSerializer(GetType(cEmploymentDetail))
Dim ns As New XmlSerializerNamespaces()
ns.Add("","")

Dim settings As New XmlWriterSettings()
settings.OmitXmlDeclaration = True

Using ms As New MemoryStream(),_
    sw As XmlWriter = XmlWriter.Create(ms,settings),_
    sr As New StreamReader(ms)
xs.Serialize(sw,obj,ns)
ms.Position = 0
Console.WriteLine(sr.ReadToEnd())
End Using

在C#中这样:

//Create our own namespaces for the output
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();

//Add an empty namespace and empty value
ns.Add("","");

//Create the serializer
XmlSerializer slz = new XmlSerializer(someType);

//Serialize the object with our own namespaces (notice the overload)
slz.Serialize(myXmlTextWriter,someObject,ns);

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读