Hi,
I am having trouble with XMLTextWriter.WriteStartElement throwing an exception:
System.InvalidOperationException
when trying to write the second element in my XML document.
This error comes back as "The Writer is closed". I have not closed it, so I am guessing it has fallen out of scope??
I have created a class to write an XML file using XMLTextWriter as an object within my class. Below is the relevant code. I have found one other post on codeguru that was never answered with the exact same issue. Any ideas for workarounds or otherwise would be appreciated.
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
I am calling these functions after instantiating a dimension from ListXML (the name of my class) with the following code:
Dim xmloutput As New ListXML
xmloutput.CreateXML()
xmloutput.AddListMember(xmloutput.ReturnWhiteList, currentItem.SenderEmailAddress, xmloutput.ReturnAddAction)