【发布时间】:2015-12-21 12:10:03
【问题描述】:
如果 ValidationEvents 是 xml 中未在 xsd 中定义的属性或元素,我如何获取它们。
xsd:
<?xml version="1.0" encoding="utf-8"?>
<xsd:schema id="VoiceXmlTSPro"
targetNamespace="http://tempuri.org/VoiceXmlTSPro.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/VoiceXmlTSPro.xsd"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!-- Elements-->
<xsd:element name="vxml">
<xsd:complexType>
<xsd:attribute ref="base"/>
<xsd:attribute ref="lang"/>
</xsd:complexType>
</xsd:element>
<!-- End Elements-->
<!-- Attributes-->
<xsd:attribute name="base" type="xsd:anyURI">
</xsd:attribute>
<xsd:attribute name="lang" type="xsd:string">
</xsd:attribute>
<!-- End Attributes-->
</xsd:schema>
xml:
<?xml version="1.0" encoding="utf-8" ?>
<vxml application="notsupported" lang="en-US" base="http://www.zebra.com">
<unknow></unknow>
</vxml>
我想要应用程序属性和未知元素的警告。 但是这段代码没有引发任何事件。
public override void Validate(string source)
{
ValidationResults.Clear();
XmlSchemaFactory xmlSchemaFactory = new XmlSchemaFactory();
XmlReaderSettings vxmlTestSettings = new XmlReaderSettings();
vxmlTestSettings.ValidationType = ValidationType.Schema;
vxmlTestSettings.ValidationFlags = XmlSchemaValidationFlags.ProcessIdentityConstraints| XmlSchemaValidationFlags.ReportValidationWarnings ;
try
{
XmlSchema xsdSchema = xmlSchemaFactory.Create(Resource.VoiceXmlTSPro);
if (xmlSchemaFactory.HasErrors())
{
// if the schema is invalid the read wil not read
return;
}
vxmlTestSettings.Schemas.Add(xsdSchema);
ValidationEventHandler eventHandler = new ValidationEventHandler(ValidationHandler);
using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(source)))
using (XmlReader xmlReader = XmlReader.Create(stream, vxmlTestSettings))
{
XmlDocument document = new XmlDocument();
document.Load(xmlReader);
document.Validate(eventHandler);
}
}
catch (Exception ex)
{
...
}
}
【问题讨论】:
-
那么您的目标到底是什么,尝试根据 .NET 框架中的特定架构或架构集验证带有 XML 实例文档的字符串?还是使用
Validate方法?如果架构有targetNamespace="http://tempuri.org/VoiceXmlTSPro.xsd",为什么您的实例不声明该命名空间?对于您当前的实例文档,您只会收到有关根元素没有架构的警告。
标签: c# xml xsd xmldocument xml-validation