如何在 C# 中根据 DTD 验证 XML 文档?
我不想做任何花哨的事情,我只想确保文档有效,如果无效则打印错误消息。 Google 向我指出了 this,但 XmlValidatingReader 似乎已过时(至少 MonoDevelop 是这么说的)我)。
编辑:我正在尝试迈赫达德的建议,但遇到了麻烦。我想我已经掌握了大部分内容,但我在任何地方都找不到 OnValidationEvent 。我从哪里获取 OnValidationEvent?
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.DTD;
settings.ValidationEventHandler += new ValidationEventHandler(/*trouble is here*/);
XmlReader validatingReader = XmlReader.Create(fileToLoad, settings);
I don't want to do anything fancy, I just want to make sure a document is valid, and print an error message if it is not. Google pointed me to this, but it seems XmlValidatingReader is obsolete (at least, that's what MonoDevelop tells me).
Edit: I'm trying Mehrdad's tip, but I'm having trouble. I think I've got most of it, but I can't find OnValidationEvent anywhere. Where go I get OnValidationEvent from?
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.DTD;
settings.ValidationEventHandler += new ValidationEventHandler(/*trouble is here*/);
XmlReader validatingReader = XmlReader.Create(fileToLoad, settings);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
而不是创建
XmlValidatingReader
类 直接,您应该构建适当的XmlReaderSettings
对象 并将其作为参数传递给XmlReader。 Create
方法:其余不变。
PS
OnValidationEvent
是您声明的用于处理验证事件的方法的名称。显然,如果您不想订阅由XmlReader
引发的验证事件,则可以删除该行。Instead of creating
XmlValidatingReader
class directly, you should construct an appropriateXmlReaderSettings
object and pass it as an argument to theXmlReader.Create
method:The rest is unchanged.
P.S.
OnValidationEvent
is the name of the method you declare to handle validation events. Obviously, you can remove the line if you don't want to subscribe to validation events raised by theXmlReader
.ValidationEventHandler
Lambda 表达式
类型推断
ValidationEventHandler
Lambda Expressions
Type Inference
我在 DTD 验证中提到了这个示例。
https:// learn.microsoft.com/en-us/dotnet/api/system.xml.xmlreadersettings.dtdprocessing?view=netcore-3.1#input
此示例具有无效的 DTD XML,我已将其更正如下。
I've referred to this example on DTD validation.
https://learn.microsoft.com/en-us/dotnet/api/system.xml.xmlreadersettings.dtdprocessing?view=netcore-3.1#input
This example has invalid DTD XML, which I've corrected as below.
完整描述:
在 Visual Studio .NET 中,创建一个新的 Visual C# 控制台应用程序
名为
ValidateXml
的项目。在开头添加两个 using 语句Class1.cs如下:
在
Class1.cs
中,在Main
方法的开始如下:创建一个
XmlTextReader
对象以从文本中读取 XML 文档在
Main
方法中创建一个XmlValidatingReader
来按如下方式验证此 XML 数据:
XmlValidatingReader
对象的ValidationType
属性指示所需的验证类型(DTD、XDR 或
架构)。将此属性设置为 DTD,如下所示:
如果发生任何验证错误,验证读取器会生成一个
验证事件。添加以下代码来注册验证
事件处理程序(您将实现
MyValidationEventHandler
步骤7)中的方法:
添加以下代码来读取并验证 XML 文档。如果有的话
发生验证错误,调用
MyValidationEventHandler
解决错误。此方法将
isValid
设置为 false(请参阅步骤 8)。验证后可以检查
isValid
的状态来查看是否文件有效或无效。
在
Main
方法之后编写MyValidationEventHandler
方法,如下所示如下:
构建并运行应用程序。应用程序应报告 XML 文档有效。
例如:
在 Visual Studio .NET 中,修改
ProductWithDTD.xml
使其无效(例如,删除M soliman
元素)。再次运行应用程序。您应该收到以下错误消息:
full description:
In Visual Studio .NET, create a new Visual C# Console Application
project named
ValidateXml
. Add two using statements to the beginningof Class1.cs as follows:
In
Class1.cs
, declare a boolean variable namedisValid
before thestart of the
Main
method as follows:Create an
XmlTextReader
object to read an XML document from a textfile in the
Main
method, and then create anXmlValidatingReader
tovalidate this XML data as follows:
The
ValidationType
property of theXmlValidatingReader
objectindicates the type of validation that is required (DTD, XDR, or
Schema). Set this property to DTD as follows:
If any validation errors occur, the validating reader generates a
validation event. Add the following code to register a validation
event handler (you will implement the
MyValidationEventHandler
method in Step 7):
Add the following code to read and validate the XML document. If any
validation errors occur,
MyValidationEventHandler
is called toaddress the error. This method sets
isValid
to false (see Step 8).You can check the status of
isValid
after validation to see if thedocument is valid or invalid.
Write the
MyValidationEventHandler
method after theMain
method asfollows:
Build and run the application. The application should report that the XML document is valid.
e.g.:
In Visual Studio .NET, modify
ProductWithDTD.xml
to invalidate it (for example, delete the<AuthorName>M soliman</AuthorName>
element).Run the application again. You should receive the following error message: