From cc767e026ec253c0d6468e81f9f1725c933bf37a Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Mon, 18 Sep 2006 19:47:59 +0000 Subject: [PATCH] Fixed SD2-1061. Creating an XML schema from XML that had a child element with the same name as one of its parents failed due to a limitation of the .NET Framework's DataSet.InferXmlSchema method. Changed the code so it now uses the XmlSchemaInference class (new in .NET 2.0). This class handles multiple namespaces and will generate a schema for each namespace. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1822 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Project/Src/CreateSchemaCommand.cs | 28 +++++++-------- .../XmlEditor/Project/Src/XmlView.cs | 36 ++++++++++++------- 2 files changed, 36 insertions(+), 28 deletions(-) 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(); + } } }