【问题标题】:Attempting to serialize a class without a namespace or declaration试图序列化一个没有命名空间或声明的类
【发布时间】:2021-12-27 15:45:31
【问题描述】:

我一直在关注How can I make the xmlserializer only serialize plain xml? 上的解决方案,试图仅序列化纯文本,但是在 VB.net 端执行此操作时遇到了一些问题

目的是防止显示行<?xml version="1.0" encoding="utf-16"?>和属性xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

我有一个子如下:

  Private Sub writeXMLContent()
        For Each dataItem As dataClass In dataSet            
            Dim emptyNameSpace As XmlSerializerNamespaces = New XmlSerializerNamespaces({XmlQualifiedName.Empty})
            Dim serializer As New XmlSerializer(GetType(dataClass))
            Dim settings As New XmlWriterSettings
            settings.Indent = True
            settings.OmitXmlDeclaration = True

            Using stream As New StringWriter
                Using writer = XmlWriter.Create(stream, settings)
                    serializer.Serialize(writer, serializer, emptyNameSpace)
                    'will write each line to a file here
                End Using
            End Using
        Next
    End Sub

但是我一直遇到同样的两个错误:

  1. Using writer = XmlWriter.Create(stream, settings) 行使用 object 类型的操作数时抛出错误必须实现 system.iDisposable
  2. serializer.Serialize(writer, serializer, emptyNameSpace) 行似乎不像我的第二个参数,因为它需要一个对象?我不太确定我会在这里传递什么对象?

【问题讨论】:

    标签: xml vb.net


    【解决方案1】:
    1. XmlWriter 没有实现IDisposable,即它没有Using statement 可以调用的Dispose 方法。只需不使用 Using 语句来修复它。

      Dim writer = XmlWriter.Create(stream, settings)
      
    2. 第二个参数必须是你想要serialize的对象,即在这种情况下可能是dataItem

      serializer.Serialize(writer, dataItem)
      

    至于删除命名空间和注释,这里有一个解决方案:

    Sub Test()
        Dim dataItem = New DataClass With {.Id = 5, .Name = "Test"}
    
        ' Serialize.
        Dim serializer As New XmlSerializer(GetType(DataClass))
        Dim sb As New StringBuilder()
        Using writer As New StringWriter(sb)
            serializer.Serialize(writer, dataItem)
        End Using
    
        Dim xml = RemoveNamespaces(sb.ToString())
    
        Console.WriteLine(xml)
    End Sub
    
    Private Function RemoveNamespaces(ByVal xml As String) As String
        Dim doc = New XmlDocument()
        doc.LoadXml(xml)
    
        ' This assumes that we have only a namespace attribute on the root element.
        doc.DocumentElement.Attributes.RemoveAll()
    
        Dim settings As New XmlWriterSettings With {.Indent = True, .OmitXmlDeclaration = True}
        Dim sb As New StringBuilder()
        Using stringWriter As New StringWriter(sb)
            Using writer = XmlWriter.Create(stringWriter, settings)
                doc.WriteTo(writer)
            End Using
        End Using
        Return sb.ToString()
    End Function
    

    它正在使用这个测试类

    Public Class DataClass
        Public Property Id As Integer
        Public Property Name As String
    End Class
    

    【讨论】:

    • 非常感谢!一旦作者的更改完成 - 序列化程序很高兴,所以它似乎以某种方式链接!
    猜你喜欢
    • 2014-01-09
    • 2013-01-10
    • 2011-10-08
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 2020-07-20
    • 2011-06-14
    • 2010-11-25
    相关资源
    最近更新 更多