Share via

Validate Xml file

StewartBW 1,905 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 :)

Developer technologies | VB
Developer technologies | C#
Developer technologies | 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.

0 comments No comments

Answer accepted by question author
  1. Jiachen Li-MSFT 34,241 Reputation points Microsoft External Staff
    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

Your answer

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