XmlDocument to XmlReader?

StewartBW 1,145 Reputation points
2024-07-13T23:12:42.57+00:00

Hello

I used to load an XML file into an XDocument and keep it intact in memory, then pass it to another object when needed, since it only accepts XmlReader, I was using XDocument.CreateReader.

Now am moving from XDocument to XmlDocument, just no idea how to pass the XmlDocument as XmlReader.

Found this snippet on a website and am not sure if this is the correct way to go?

It uses XmlNodeReader not XmlReader, and although it is accepted, the XML data will be intact and this will result the same as using XDocument.CreateReader?

Thanks in advance :)

Dim xml As New XmlDocument()
Using xnr As New XmlNodeReader(xml)
	Pass xnr as XmlReader ???
End Using
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,001 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,733 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 32,056 Reputation points Microsoft Vendor
    2024-07-15T07:49:29.19+00:00

    Hi @StewartBW ,

    Yes, using XmlNodeReader to create an XmlReader from an XmlDocument is a valid approach. XmlNodeReader implements XmlReader, so it can be used in places where an XmlReader is required. This will ensure that the XML data remains intact.

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  2. StewartBW 1,145 Reputation points
    2024-07-14T17:18:29.11+00:00

    How am I supposed to see the mistakenly removed comments/answers? :(

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.