c#XML Schemavalidation

我有一个很好的XML文件,如下所示:

   Boomerang - Error codes.xlsx Boomerang - Error codes This is the Boomerang error codes file  Excel Boomerang   1 4    Issue Tracker v5.xlsx This is the issue tracker for Skipstone  Excel Skipstone   1 4    

然后我有我创建的架构,如下所示:

                               

据我所知,xml文件无效,因为第一个元素是Assetd而不是Asset,但如果我运行我的c#代码:

 XmlSchemaSet schemas = new XmlSchemaSet(); schemas.Add("http://tempuri.org/data.xsd", "data.xsd"); XDocument doc = XDocument.Load(openFileDialog1.FileName); string msg = ""; doc.Validate(schemas, (o, err) => { msg = err.Message; }); Console.WriteLine(msg == "" ? "Document is valid" : "Document invalid: " + msg); 

它告诉我xml是有效的……如果我使用这段代码:

 // Set the validation settings. XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas.Add("http://tempuri.org/data.xsd", "data.xsd"); settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessInlineSchema; settings.ValidationFlags |= XmlSchemaValidationFlags.ProcessSchemaLocation; settings.ValidationFlags |= XmlSchemaValidationFlags.ReportValidationWarnings; settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack); // Create the XmlReader object. XmlReader reader = XmlReader.Create(openFileDialog1.FileName, settings); // Parse the file. while (reader.Read()) ; 

我在控制台中得到了这个输出:

 Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Assets'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the attribute 'Path'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Assetd'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'FileName'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'DisplayName'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Description'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tags'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tag'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tag'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Categories'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Category'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Category'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Asset'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'FileName'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Description'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tags'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tag'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Tag'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Categories'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Category'. Warning: Matching schema not found. No validation occurred. Could not find schema information for the element 'Category'. 

谁能告诉我我做错了什么? 它杀了我:(

欢呼,/ r3plica

您需要在xml中设置默认命名空间,如下所示:

    Boomerang - Error codes.xlsx Boomerang - Error codes This is the Boomerang error codes file  Excel Boomerang   1 4    Issue Tracker v5.xlsx This is the issue tracker for Skipstone  Excel Skipstone   1 4    

此外,还有许多其他问题:

路径属性未在架构中定义,未定义“Assetd”元素。 需要在模式中为xs设置maxOccurs =“unbounded”:element name =“Asset”

如果您无法修改xml,则需要从xsd中删除目标模式:

  

并注册这样的架构:

 settings.Schemas.Add(null, "data.xsd");