sign in | join about | help | sitemap
Connecting Tech Pros Worldwide
dickster's Avatar

No XmlNodeType for an EmptyElement with Attribute


Question posted by: dickster (Guest) on November 12th, 2005 05:08 AM
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

2 Answers Posted
Martin Honnen's Avatar
Guest - n/a Posts
#2: Re: No XmlNodeType for an EmptyElement with Attribute



dickster wrote:

[color=blue]
> 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
>[/color]

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.
[color=blue]
> Case XmlNodeType.EndElement
> myWriter.WriteEndElement()[/color]


--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
dickster's Avatar
Guest - n/a Posts
#3: Re: No XmlNodeType for an EmptyElement with Attribute

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

 
Not the answer you were looking for? Post your question . . .
196,842 members ready to help you find a solution.
Join Bytes.com

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 196,842 network members.
Post your question now . . .
It's fast and it's free

Popular Articles

Top Community Contributors