How to validate an XML document?

纵然是瞬间 提交于 2019-12-18 04:09:23

问题


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#?


回答1:


You can validate xml files against XSD.

  • First you have to create Xml Schema Definition file. See example
  • use XML Schema Definition Tool to create XSD from XMLfile
  • Use this code to validate input XML using corresponding 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.




回答2:


Validating Input Xml Data Files



来源:https://stackoverflow.com/questions/3465335/how-to-validate-an-xml-document

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!