How to validate an XML document?
Asked Answered
I

2

9

My C#/.NET application reads XML files that are manually edited by the users. The allowed elements and tags are described in the application's documentation. I'm using LINQ to extract data from the XML file.

Before extracting data from the XML file, I'd like to validate it to see if it has the expected structure. If not, it would be nice to have information about what is wrong so that I can give some feeback to the user.

What's the simplest way to do this in C#?

Immature answered 12/8, 2010 at 6:51 Comment(0)
K
9

You can validate xml files against XSD.

Hope this will help...

EDIT

This article explains all possible ways to validate xml, using C#
How To Validate an XML Document by Using DTD, XDR, or XSD in Visual C# .NET

IMO best option is to use XSD.

Kufic answered 12/8, 2010 at 6:59 Comment(3)
...and depending on the editor you users are using, they may even be able to get Intellisense when editing the XML file when the XSD is available.Moran
this should be enough to get me started. Thanks a lot!Immature
XmlValidatingReader in the example code (step 3) is deprecated according to .NET 4.0 and possibly earlier.Katelynnkaterina
S
0

Validating Input Xml Data Files

Sponsor answered 12/8, 2010 at 6:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.