这是我的功能。
如果将 MemoryStream 传递给 XmlReader,有时它不会验证正确的 xml 文件。我将 XmlDocument 对象存储在内存中,我想根据最终用户提供的 xsd 架构文件对其进行验证。
ValidateSchema1(string XMLPath, string XSDPath)
{
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.Load(XMLPath);
using (MemoryStream mstream = new MemoryStream())
{
//StreamWriter writer = new StreamWriter(mstream);
xmlDocument.Save(mstream);
mstream.Seek(0, SeekOrigin.Begin);
XmlSchemaSet sc = new XmlSchemaSet();
// Add the schema to the collection.
sc.Add(null, XSDPath);
// Set the validation settings.
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas = sc;
settings.ValidationEventHandler += ValidationCallBack;
// Create the XmlReader object.
// Not woking
XmlReader reader = XmlReader.Create(mstream, settings);
// Working
//XmlReader reader = XmlReader.Create(new StringReader(xmlDocument.InnerXml), settings);
// Working
//XmlReader reader = XmlReader.Create(XMLPath, settings);
// Parse the file.
while (reader.Read()) ;
}
}
这可能有效:http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_23387252.html http://www.experts-exchange.com/Programming/Languages/.NET/Visual_CSharp/Q_23387252.html
这有效如何在运行时根据 xsd 验证 xml,而不将 xsd 文件保存在本地文件夹上? https://stackoverflow.com/questions/3085526/how-to-validate-on-runtime-xml-against-xsd-without-save-the-xsd-file-on-local
编辑 1:修复了您提供的代码,现在代码可以正常工作,验证了我的 2 个文件。您收到错误的原因是您尝试使用自身验证 Xsd 而根元素不存在。请亲自查看解决方案。
public void Xsd_whithout_saved()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(@"file.xsd");
//In the futute, strArquivoInteiro will be fullfill by xsd comming from database as nvarchar(max) and I will //not be allowed to save as a file locally
string strArquivoInteiro = xmlDoc.OuterXml;
byte[] byteArray = Encoding.ASCII.GetBytes(strArquivoInteiro);
MemoryStream streamXSD = new MemoryStream(byteArray);
//<<<
streamXSD.Position = 0;
StreamReader readerXsd = new StreamReader(streamXSD);
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationEventHandler += this.MyValidationEventHandler;
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, XmlReader.Create(readerXsd));
settings.CheckCharacters = true;
XmlReader XmlValidatingReader = XmlReader.Create(@"file.xml", settings);
XmlTextReader Reader = new XmlTextReader(@"file.xsd");
//Created another reader for xml to use for validation
XmlTextReader Reader2 = new XmlTextReader(@"file.xml");
XmlSchema Schema = new XmlSchema();
//IN THIS LINE I RECEIVED THE XmlException "Root Element is Missing" and I can't understand the reason
//This was the first problem, a xsd root element isn't equal to an xml root element , and you where trying to validate and xsd with xsd here, and of course the error.
Schema = XmlSchema.Read(Reader, MyValidationEventHandler);
XmlValidatingReader ValidatingReader = new XmlValidatingReader(Reader2);
ValidatingReader.ValidationType = ValidationType.Schema;
ValidatingReader.Schemas.Add(Schema);
try
{
XmlValidatingReader.Read();
XmlValidatingReader.Close();
ValidatingReader.ValidationEventHandler += MyValidationEventHandler;
while ((ValidatingReader.Read()))
{
}
ValidatingReader.Close();
}
catch (Exception ex)
{
ValidatingReader.Close();
XmlValidatingReader.Close();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)