问题描述
据我所知,对于XML验证,"找不到元素"错误已经有很多现有的线程,但是我发布这个问题,因为我的问题略有不同。
I understand that there are already many existing threads for the 'element not found' error for XML validation but I'm posting this as my problem is slightly different.
我的代码运行正常,直到它在.Net v4.5上。在将框架升级到v4.6.1之后,我开始在我的xml文件中为每个XML元素获取此错误。降级后,我发现错误从.Net framework v4.5.2开始发生。
以下是我的代码:
My code was running fine until it was on .Net v4.5. After upgrading the framework to v4.6.1, I started getting this error for each XML element in my xml file. Upon downgrading I found that the error started occurring from .Net framework v4.5.2 onwards. Below is my code:
XML - Book.xml
<?xml version="1.0" ?>
<!DOCTYPE book SYSTEM "Book.dtd">
<book>
<title>The Lord of the Rings</title>
<author>J.R.R. Tolkien</author>
<isbn>1572810556</isbn>
</book>
DTD - Book.dtd
DTD - Book.dtd
<!ELEMENT book (title, author, isbn)>
<!ELEMENT title (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT isbn (#PCDATA)>
C#函数
public static void ReadXMLwithDTD()
{
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.DTD;
settings.DtdProcessing = DtdProcessing.Parse;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
settings.IgnoreWhitespace = true;
// Create the XmlReader object.
XmlReader reader = XmlReader.Create("Book.xml", settings);
// Parse the file.
while (reader.Read())
{
Console.WriteLine("{0}, {1}: {2} ", reader.NodeType, reader.Name, reader.Value);
}
}
private static void ValidationCallBack(object sender, ValidationEventArgs e)
{
if (e.Severity == XmlSeverityType.Warning)
Console.WriteLine("Warning: Matching schema not found. No validation occurred." + e.Message);
else // Error
Console.WriteLine("Validation error: " + e.Message);
}
执行代码时,回调会引发所有四个XML元素的验证错误(未找到元素) - 书籍,标题,作者和
isbn。
我还发现DTD没有被读取。我在dtd文件中引入了错误,甚至重命名了文件,没有任何变化。接下来,我恢复了错误并将框架切换回4.5,代码按预期工作。由于框架仍然设置为4.5,我重新引入了
的错误,并且代码在适用时抛出了错误。
I also found that DTD is not read. I introduced errors in the dtd file, even renamed the file, there was no change. Next, I reverted the errors and switched the framework back to 4.5, the code worked as expected. With the framework still set at 4.5, I re-introduced the errors and the code threw errors as applicable.
请帮助,我需要紧急解决这个问题。
谢谢,
推荐答案
这个论坛是关于C#编程语言,IDE,库,示例和工具。由于
你的问题与在XML开发中,我会帮助您将此线程移至"XML,System.Xml,MSXML和XmlLite"论坛以获得更好的支持。
This forum is about the C# programming language, IDE, libraries, samples, and tools. Asyour issue is more related to the XML development, I would help you move this thread to the "XML, System.Xml, MSXML and XmlLite" forum for better support.
感谢您的理解。
最好的问候,
Albert
张
这篇关于从.Net v4.5升级到.Net v4.5.2后,元素未声明验证错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!