Validate Xml file

StewartBW 845 Reputation points
2024-07-11T02:55:36.5266667+00:00

Hello,

I just need to verify if a file is xml, no specific structure, so just loading to an xml parser and if no exception is thrown at load, I'm fine with it, so I use:

Dim doc As XDocument
doc = XDocument.Load(InputStream, LoadOptions.None)
Check for exceptions
doc = Nothing

Now, is it the same as using the below code?

Dim doc As New XmlDocument
doc.Load(InputStream)
Check for exceptions
doc = Nothing

Any difference in loading behaviors of XDocument vs XmlDocument?

Thanks :)

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.
10,619 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,659 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 28,946 Reputation points Microsoft Vendor
    2024-07-11T05:30:33.79+00:00

    Hi @StewartBW ,

    For the specific task of verifying if a file is XML by attempting to load it and catching exceptions, both XDocument and XmlDocument will achieve the same result.

    But XDocument provides options like LoadOptions to control certain behaviors (e.g., preserving white space, setting base URI, etc.).

    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.
    0 comments No comments

0 additional answers

Sort by: Most helpful