我正在修复程序中的警告,而且很明显,xmlvalidating和xmlvalidating收集已经过时了。问题是,我不太确定怎么做。下面是一个尝试,用包含xmlschemaset和xmlreader.create的新验证函数“模拟”前面的验证函数。我首先声明一个模式,并使用targeturi字符串设置它,然后在设置验证事件处理程序时将它添加到模式集中。我认为我的问题是设置读取器和输入流。我知道如何使用xmlvalidating来完成这个任务,但是如果我想修复这些警告,这不是一个选项。这是代码和尝试。在测试期间,只使用新的验证xml代码,将旧的xml代码注释掉。
// New Validation Xml.
string xsd_file = filename.Substring(0, filename.Length - 3) + "xsd";
XmlSchema xsd = new XmlSchema();
xsd.SourceUri = xsd_file;
XmlSchemaSet ss = new XmlSchemaSet();
ss.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
ss.Add(xsd);
if (ss.Count > 0)
{
XmlTextReader r = new XmlTextReader(filename2);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(ss);
settings.ValidationEventHandler +=new ValidationEventHandler(ValidationCallBack);
XmlReader reader = XmlReader.Create(filename2, settings);
while (reader.Read())
{
}
reader.Close();
}
// Old Validate XML
XmlSchemaCollection sc = new XmlSchemaCollection();
sc.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
sc.Add(null, xsd_file);
if (sc.Count > 0)
{
XmlTextReader r = new XmlTextReader(filename2);
XmlValidatingReader v = new XmlValidatingReader(r);
v.ValidationType = ValidationType.Schema;
v.Schemas.Add(sc);
v.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
while (v.Read())
{
}
v.Close();
}
private void ValidationCallBack(object sender, ValidationEventArgs e)
{
// If Document Validation Fails
isvalid = false;
MessageConsole.Text = "INVALID. Check message and datagridview table.";
richTextBox1.Text = "The document is invalid: " + e.Message;
}不幸的是,当我运行这个程序并试图验证一个无效的xml文档时,它会给我一个错误:“'URNLookup‘元素没有声明。”URNLookup元素是xml文件的根元素。我总是可以回到以前的验证方法,但是那些警告让我感到害怕。
任何帮助都是非常感谢的。提前谢谢你!如果我错过了任何信息,我很乐意提供更多的信息。
发布于 2011-06-03 14:32:41
我已经解决了这个问题,现在它又在没有警告的情况下工作了。在新验证XML中:
// New Validation Xml.
string xsd_file = filename.Substring(0, filename.Length - 3) + "xsd";
XmlSchema xsd = new XmlSchema();
xsd.SourceUri = xsd_file;
XmlSchemaSet ss = new XmlSchemaSet();
ss.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
ss.Add(null, xsd_file);
if (ss.Count > 0)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(ss);
settings.Schemas.Compile();
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
XmlTextReader r = new XmlTextReader(filename2);
using (XmlReader reader = XmlReader.Create(r, settings))
{
while (reader.Read())
{
}
}
}ss.add被更改为有一个名称空间和文件字符串。添加了settings.schemas.compile(),并对“使用(xmlreader .”)进行了不重要的重组。被添加了。
这个页面帮了我很大的忙:http://msdn.microsoft.com/en-us/library/fe6y1sfe(v=vs.80).aspx,它现在起作用了。
https://stackoverflow.com/questions/6207971
复制相似问题