all groups > dotnet xml > september 2005 >
You're in the

dotnet xml

group:

No XmlNodeType for an EmptyElement with Attribute



No XmlNodeType for an EmptyElement with Attribute dickster
9/30/2005 9:11:22 AM
dotnet xml: I have a MemoryStream of XML in which I wish to strip out the
namespaces and some other attributes and then write this revised Stream
to a new MemoryStream

Everything works grand in the code below expect when myReader comes
across an element like:
<xxx id="1"/>
i.e. an empty element with attributes

I think myReader.IsEmptyElement is no use in this instance as there is
an attribute in the "EmptyElement"

Heres the guts of the code:
**********************************************************************
Dim myReader As New XmlTextReader(p_Stream)
'Where p_Stream is the original MemoryStream of XML
'Where outputStream is the target MemoryStream of XML
Dim myWriter As New XmlTextWriter(outputStream, Encoding.UTF8)

While myReader.Read
Select Case myReader.NodeType
Case XmlNodeType.Element

' in here i check if there are attributes
' & strip out the ones i dont wont using
' myReader.MoveToNextAttribute &
' WriteAttributeString where appropriate.
' <xxx id="1"/> is caught in here
' but the associated end element is
' never moved to by myReader.Read
' - so eventually the xml is badly formed

Case XmlNodeType.EndElement
myWriter.WriteEndElement()
Case XmlNodeType.Text
myWriter.WriteString(myReader.Value)
Case XmlNodeType.SignificantWhitespace
myWriter.WriteString(myReader.Value)
Case XmlNodeType.XmlDeclaration
myWriter.WriteStartDocument()
Case Else
'Do nothing
End Select
End While

**********************************************************************

Perhaps there is a better way to go about this altogether.
I'm open to all options.

Dickster
Re: No XmlNodeType for an EmptyElement with Attribute Martin Honnen
9/30/2005 7:28:39 PM


[quoted text, click to view]


[quoted text, click to view]

You need to check
myReader.IsEmptyElement
here as the markup <xxx id="1"/> should give you true in that case while
the reader will not give you an EndElement for that markup.

[quoted text, click to view]


--

Martin Honnen --- MVP XML
Re: No XmlNodeType for an EmptyElement with Attribute dickster
10/3/2005 2:17:44 AM
Thanks Again Martin.. Heres my working code
============================================================

While myReader.Read

If myReader.IsEmptyElement() Then
myWriter.WriteStartElement(myReader.Name)

' Handle attributes

myWriter.WriteEndElement()

Else
Select Case myReader.NodeType
Case XmlNodeType.Element

myWriter.WriteStartElement(myReader.Name)
' Handle attributes

Case XmlNodeType.EndElement
myWriter.WriteEndElement()
Case XmlNodeType.Text
myWriter.WriteString(myReader.Value)
Case XmlNodeType.SignificantWhitespace
myWriter.WriteString(myReader.Value)
Case XmlNodeType.XmlDeclaration
myWriter.WriteStartDocument()
Case XmlNodeType.CDATA
myWriter.WriteCData(myReader.Value)
Case XmlNodeType.EntityReference
myWriter.WriteEntityRef(myReader.Name)
Case XmlNodeType.ProcessingInstruction
' Handle this
Case Else
'Do nothing
End Select
End If

myWriter.Flush()
End While
AddThis Social Bookmark Button