0
如何獲取ValidationEvents如果theire是xml中未定義在xsd中的屬性或元素。XmlDocument.Validate不支持的屬性或元素
的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>
我要爲應用性質和對不明ellement警告。 但是這段代碼並沒有拖延任何事件。
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)
{
...
}
}
那麼,什麼是你的目標恰好,試圖使用XML實例文檔驗證字符串是否符合.NET框架中設置的特定模式或模式?或者使用'Validate'方法?如果模式具有'targetNamespace =「http://tempuri.org/VoiceXmlTSPro.xsd」',爲什麼你的實例不聲明那個名字空間?使用您當前的實例文檔,您只會收到有關根元素沒有模式的警告。 –