public Task <XsdDocument> ParseAsync(string path) { // Load the XSD into a schema set and compile it up var schemaSet = new XmlSchemaSet(); AddSchemaToSet(schemaSet, path); schemaSet.Compile(); // Create the doc var doc = new XsdDocument(); // Set up the context we'll pass around to keep parameter lists shorter var context = new Context { SchemaSet = schemaSet, Document = doc }; // Go through all the global elements and process them. foreach (XmlSchemaElement element in schemaSet.GlobalElements.Values) { try { ProcessElement(context, doc, element); } catch (Exception ex) { throw new ParserException(element, $"Unhandled exception parsing element: {element.Name}", ex); } } return(Task.FromResult(doc)); }
private void ProcessElement(Context context, XsdDocument doc, XmlSchemaElement xmlSchemaElement) { var xsdElement = doc.FindOrCreateElement(xmlSchemaElement.Name); if (xmlSchemaElement.ElementSchemaType is XmlSchemaComplexType complex) { ProcessComplexType(context, xsdElement, complex); } else if (xmlSchemaElement.ElementSchemaType is XmlSchemaSimpleType simple) { ProcessSimpleType(xsdElement, simple); } else { throw new ParserException(xmlSchemaElement, $"Unexpected ElementSchemaType: {xmlSchemaElement.ElementSchemaType}."); } }
public async Task <XsdDocument> RunAsync() { var rows = await Input.RunAsync(); var doc = new XsdDocument(); // Process all the input rows foreach (var row in rows) { var element = doc.FindOrCreateElement(row.ElementName); if (!string.IsNullOrEmpty(row.ChildElementName)) { // This row applies to a child element... var child = element.FindOrCreateElement(row.ChildElementName); child.DataType = row.DataType; child.MinOccurs = row.MinOccurs; child.MaxOccurs = row.MaxOccurs; if (child.DataType == "ref") { child.RefName = row.ChildElementName; } else { ProcessEnums(child, row); } } else if (!string.IsNullOrEmpty(row.AttributeName)) { // This row applies to a child attribute... var attribute = element.FindOrCreateAttribute(row.AttributeName); attribute.DataType = row.DataType; attribute.MaxLength = row.MaxLength; attribute.Comment = row.Comment; ProcessEnums(attribute, row); if (!string.IsNullOrEmpty(row.Required)) { attribute.MaxOccurs = "1"; if (row.Required.Equals("required", StringComparison.OrdinalIgnoreCase)) { attribute.MinOccurs = "1"; } else { attribute.MinOccurs = "0"; } } } else { // This row applies to the element itself element.DataType = row.DataType; element.MaxLength = row.MaxLength; element.Comment = row.Comment; } } // If a debug path was specified, write the doc there for post-mortem analysis... if (!string.IsNullOrEmpty(DebugDumpPath)) { using (var stream = new FileStream(DebugDumpPath, FileMode.Create, FileAccess.Write)) { var jsonOptions = new JsonSerializerOptions { WriteIndented = true }; try { await JsonSerializer.SerializeAsync(stream, doc, jsonOptions); logger.LogDebug("Wrote CsvToXsdTranslator intermediate debug output to {Path}.", DebugDumpPath); } catch (JsonException ex) { logger.LogWarning("JSON Exception saving CsvToXsd debug dump: {Message}", ex.Message); } } } // Return what we've built return(doc); }
private XsdElement GetVerifiedElement(XsdDocument doc, string elementName) { doc.Elements.Should().Contain(x => x.Name == elementName); return(doc.Elements.First(x => x.Name == elementName)); }