我遇到了 XMLTextWriter.WriteStartElement 引发异常的问题:
System.InvalidOperationException
尝试在我的 XML 文档中编写第二个元素时。
此错误返回为"The Writer is closed"。我还没有关闭它,所以我猜它已经超出了范围??
我创建了一个类来使用 XMLTextWriter 作为我类中的对象来编写 XML 文件。下面是相关代码。我在codeguru上发现了另一篇从未用完全相同的问题回答的帖子。任何有关变通办法或其他方法的想法将不胜感激。
Function CreateXML()...
Try
_listDocument = New XmlTextWriter(_xmlDI.FullName & "\\" & currentFilename, Nothing)
CreateHeader()
AddTimeDateNode()
CreateXML = True
Catch xmlErr As XmlException
MsgBox("Unable to create temporary file(" & currentFilename & ") that is used to change your whitelist or blacklist. " & _
"More technical information: " & xmlErr.Message, MsgBoxStyle.Critical, "Can't Continue")
End Try
End Function
Function AddListMember(ByVal listType As String, ByVal listItem As String, ByVal action As String) As Boolean
_listDocument.WriteStartElement(listItem) <-- CODE THROWS EXCEPTION HERE!
_listDocument.WriteAttributeString("listType", listType)
_listDocument.WriteAttributeString("action", action)
_listDocument.WriteString(listItem)
_listDocument.WriteEndElement()
_listDocument.WriteWhitespace(Chr(13) & Chr(10) & "\t")
Return True
End Function
'Sets the XML header
Private Function CreateHeader() As Boolean
_listDocument.WriteStartDocument(False)
_listDocument.WriteWhitespace(Chr(13) & Chr(10))
Return True
End Function
'Add Time Date node
Private Function AddTimeDateNode() As Boolean
_listDocument.WriteStartElement("DateTimeAdded")
_listDocument.WriteString(DateTime.Now.ToString)
_listDocument.WriteEndElement()
_listDocument.WriteWhitespace(Chr(13) & Chr(10))
Return True
End Function
我在使用以下代码从 ListXML(我的类的名称)实例化一个维度后调用这些函数:
Dim xmloutput As New ListXML
xmloutput.CreateXML()
xmloutput.AddListMember(xmloutput.ReturnWhiteList, currentItem.SenderEmailAddress, xmloutput.ReturnAddAction)