C# 如何使用xmlschemaset和xmlreader.create根据xsd模式验证xml
我正在修复程序中的警告,显然xmlvalidating reader和xmlschemacollection已经过时。问题是,我不太清楚怎么做。这里尝试用新的验证函数“模仿”以前的验证函数,该函数涉及xmlschemaset和xmlreader.create。我首先声明一个模式,并使用targeturi字符串对其进行设置,然后在设置验证事件处理程序时将其添加到schemaset中。我认为我的问题是设置阅读器和输入流。我知道如何使用xmlvalidating reader进行验证,但如果我想修复这些警告,这不是一个选项。下面是代码和尝试。在测试期间,只使用了新的验证xml代码,旧的代码被注释掉C# 如何使用xmlschemaset和xmlreader.create根据xsd模式验证xml,c#,validation,xsd,C#,Validation,Xsd,我正在修复程序中的警告,显然xmlvalidating reader和xmlschemacollection已经过时。问题是,我不太清楚怎么做。这里尝试用新的验证函数“模仿”以前的验证函数,该函数涉及xmlschemaset和xmlreader.create。我首先声明一个模式,并使用targeturi字符串对其进行设置,然后在设置验证事件处理程序时将其添加到schemaset中。我认为我的问题是设置阅读器和输入流。我知道如何使用xmlvalidating reader进行验证,但如果我想修复这
// 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文件的根元素。我总是可以回到旧的验证方法,但是那些警告让我害怕
非常感谢您的帮助。提前谢谢你!如果我遗漏了任何信息,我很乐意提供更多信息
- tf.rz(.NET3.5SP1,VisualStudioC#2008)
// 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(),并添加了“using(xmlreader….)”的无关紧要的重组
这个页面帮助了我很多:它现在可以工作了。我们如何将它用于嵌入式资源XSD?