tags:

views:

1693

answers:

1

In the following snippet, using XmlReader, when I encounter an element. I would like to read it as-is, including all attributes and namespace decoration in the element. Using the oXml.Name property, I am only able to get the tag name. Is there a function to get the tag itself?

oXml = XmlReader.Create(path, oXmlSettings)
While oXml.Read()
   Select Case oXml.NodeType
      Case XmlNodeType.Element
        'Read Element as-is
         If taglist.contains(oXml.Name)
           stringbuilder.Append(oXml.ReadOuterXml())
         End If
      Case XmlNodeType.Text
         stringbuilder.Append(oXml.Value)
   End Select
End While

I have tried oXml.ReadOuterXml() but it returns the element and its subcontent. That could be acceptable, but how do I fast forward my XmlReader to ignore subsequent XmlNodeType.Text and XmlNodeType.EndElement that will happen when the element I just got from ReadOuterXml has been parsed?

UPDATED: For the following snippet, loc1 is in taglist, so is written using ReadOuterXml, but the parser fails to get the following "!" character.

<para>
  Test blabla <loc1 href="test">complicated</loc1>!
</para>
+1  A: 

I haven't tried it, but I wouldn't expect that you'd have to fast forward the reader after calling ReadOuterXml. I'd expect the act of reading the outer XML to consume it.

Have you tried it?

Jon Skeet
Yes it does fast forward, but I believe it misses the event next after the EndElement. Is it because I already did oXml.Read()?
Vincent
Are you calling Read again *after* ReadOuterXml? If so, that's the call which is hurting you.
Jon Skeet
Please see the updated code snippet, I believe Read is effectively called after?
Vincent
You're going round the loop again, so yes, it will call Read again. Look at the current node immediately after calling ReadOuterXml.
Jon Skeet