diff --git a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/CreateSchemaCommand.cs b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/CreateSchemaCommand.cs index 43843c1083..458d876ba1 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/CreateSchemaCommand.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/CreateSchemaCommand.cs @@ -32,11 +32,13 @@ namespace ICSharpCode.XmlEditor XmlView xmlView = XmlView.ActiveXmlView; if (xmlView != null) { // Create a schema based on the xml. - string schema = xmlView.CreateSchema(); - if (schema != null) { - // Create a new file and display the generated schema. - string fileName = GenerateSchemaFileName(xmlView.TextEditorControl.FileName); - OpenNewXmlFile(fileName, schema); + string[] schemas = xmlView.InferSchema(); + if (schemas != null) { + // Create a new file for each generated schema. + for (int i = 0; i < schemas.Length; ++i) { + string fileName = GenerateSchemaFileName(xmlView.TextEditorControl.FileName, i + 1); + OpenNewXmlFile(fileName, schemas[i]); + } } } } @@ -50,22 +52,16 @@ namespace ICSharpCode.XmlEditor } /// - /// Generates an xsd filename based on the name of the original xml - /// file. If a file with the same name is already open in SharpDevelop - /// then a new name is generated (e.g. MyXml1.xsd). + /// Generates an xsd filename based on the name of the original xml file. /// - string GenerateSchemaFileName(string xmlFileName) + string GenerateSchemaFileName(string xmlFileName, int count) { string baseFileName = Path.GetFileNameWithoutExtension(xmlFileName); string schemaFileName = String.Concat(baseFileName, ".xsd"); - - int count = 1; - while (FileService.IsOpen(schemaFileName)) { - schemaFileName = String.Concat(baseFileName, count.ToString(), ".xsd"); - ++count; + if (count == 1) { + return schemaFileName; } - - return schemaFileName; + return schemaFileName = String.Concat(baseFileName, count.ToString(), ".xsd"); } } } diff --git a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs index 3251d3cc8a..a2c639f214 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs @@ -377,23 +377,17 @@ namespace ICSharpCode.XmlEditor /// /// Creates a schema based on the xml content. /// - /// A generated schema or null if the xml content is not + /// A set of generated schemas or null if the xml content is not /// well formed. - public string CreateSchema() + public string[] InferSchema() { TaskService.ClearExceptCommentTasks(); if (IsWellFormed) { - string schema; - using (DataSet dataSet = new DataSet()) { - dataSet.ReadXml(new StringReader(Text), XmlReadMode.InferSchema); - EncodedStringWriter writer = new EncodedStringWriter(xmlEditor.TextEditorProperties.Encoding); - XmlTextWriter xmlWriter = CreateXmlTextWriter(writer); - dataSet.WriteXmlSchema(xmlWriter); - schema = writer.ToString(); - writer.Close(); - xmlWriter.Close(); + using (XmlTextReader reader = new XmlTextReader(new StringReader(Text))) { + XmlSchemaInference schemaInference = new XmlSchemaInference(); + XmlSchemaSet schemaSet = schemaInference.InferSchema(reader); + return GetSchemas(schemaSet); } - return schema; } else { ShowErrorList(); } @@ -1282,5 +1276,23 @@ namespace ICSharpCode.XmlEditor } return null; } + + /// + /// Converts a set of schemas to a string array, each array item + /// contains the schema converted to a string. + /// + string[] GetSchemas(XmlSchemaSet schemaSet) + { + List schemas = new List(); + foreach (XmlSchema schema in schemaSet.Schemas()) { + using (EncodedStringWriter writer = new EncodedStringWriter(xmlEditor.TextEditorProperties.Encoding)) { + using (XmlTextWriter xmlWriter = CreateXmlTextWriter(writer)) { + schema.Write(xmlWriter); + schemas.Add(writer.ToString()); + } + } + } + return schemas.ToArray(); + } } }