Wednesday, March 8, 2017

De-duplicating XML validator in C#

Thought I needed XML validation for a project I'm working on. Wanted to be able to merge together several different stylesheets to check against. Also kept running into an error of Wildcard '##any' allows element

Overall, this seems to kinda work, but I may or may not need it. But maybe someone else can get it to work better. Original fix for duplicates I found

Needs System.Xml.Schema & System.Collections.Generic

public static XmlSchemaSet MergeSchemaFiles(string[] schemaFiles)
{
 // Get List of Schemas
 var schemas = new List();
 int sfi = 0;
 foreach (var sf in schemaFiles) {
  var tempFileXSS = new XmlSchemaSet();
  tempFileXSS.Add(null,sf);
  schemas.Add(tempFileXSS);
  schemas[sfi].CompilationSettings.EnableUpaCheck = false;
  schemas[sfi].Compile();
  Console.WriteLine("Loading schema from: " + sf + ", with " + schemas[sfi].GlobalElements.Values.Count + " elements.");
  sfi++;
    }
    // Merge schemas into one schema set: avoid duplicates
    var tempXSS = new XmlSchemaSet();  
    tempXSS.Add(schemas[0]);  
    for (int i = 1; i < schemas.Count; i++) {
  foreach (XmlSchemaElement xse0 in schemas[0].GlobalElements.Values) {
      foreach (XmlSchemaElement xseI in schemas[i].GlobalElements.Values) {
    if (xseI.QualifiedName.Equals(xse0.QualifiedName)) {  
       ((XmlSchema)xseI.Parent).Items.Remove(xseI);  
       break;
    }
      }
  }
  foreach (XmlSchema schema in schemas[i].Schemas()) {
      schemas[i].Reprocess(schema);
  }
  schemas[i].Compile();
  tempXSS.Add(schemas[i]);
    }
    // Return results
    Console.WriteLine("Retained " + schemas.Count + " XML schemas");  
    return tempXSS;
}

No comments:

Post a Comment