diff --git a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/CreateSchemaCommand.cs b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/CreateSchemaCommand.cs index 3abaa5c800..43843c1083 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/CreateSchemaCommand.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/CreateSchemaCommand.cs @@ -10,7 +10,6 @@ using ICSharpCode.SharpDevelop.DefaultEditor.Gui.Editor; using ICSharpCode.SharpDevelop.Gui; using ICSharpCode.SharpDevelop; using System; -using System.Data; using System.IO; using System.Text; using System.Windows.Forms; @@ -32,46 +31,14 @@ namespace ICSharpCode.XmlEditor // Find active XmlView. XmlView xmlView = XmlView.ActiveXmlView; if (xmlView != null) { - // Create a schema based on the xml. - SharpDevelopTextEditorProperties properties = xmlView.TextEditorControl.TextEditorProperties as SharpDevelopTextEditorProperties; - string schema = CreateSchema(xmlView.TextEditorControl.Document.TextContent, xmlView.TextEditorControl.Encoding, properties.ConvertTabsToSpaces, properties.TabIndent); - - // Create a new file and display the generated schema. - string fileName = GenerateSchemaFileName(xmlView.TitleName); - OpenNewXmlFile(fileName, schema); - } - } - - /// - /// Creates a schema based on the xml content. - /// - /// A generated schema. - string CreateSchema(string xml, Encoding encoding, bool convertTabsToSpaces, int tabIndent) - { - string schema = String.Empty; - - using (DataSet dataSet = new DataSet()) { - dataSet.ReadXml(new StringReader(xml), XmlReadMode.InferSchema); - EncodedStringWriter writer = new EncodedStringWriter(encoding); - XmlTextWriter xmlWriter = new XmlTextWriter(writer); - - xmlWriter.Formatting = Formatting.Indented; - if (convertTabsToSpaces) { - xmlWriter.Indentation = tabIndent; - xmlWriter.IndentChar = ' '; - } else { - xmlWriter.Indentation = 1; - xmlWriter.IndentChar = '\t'; + 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); } - - dataSet.WriteXmlSchema(xmlWriter); - schema = writer.ToString(); - writer.Close(); - xmlWriter.Close(); } - - return schema; } /// diff --git a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs index 6145bb932f..1b013e7cc5 100644 --- a/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs +++ b/src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs @@ -8,6 +8,7 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Data; using System.Drawing; using System.Drawing.Printing; using System.IO; @@ -371,6 +372,32 @@ namespace ICSharpCode.XmlEditor } } + /// + /// Creates a schema based on the xml content. + /// + /// A generated schema or null if the xml content is not + /// well formed. + public string CreateSchema() + { + 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(); + } + return schema; + } else { + ShowErrorList(); + } + return null; + } + /// /// Finds the definition of the xml element or attribute under the cursor /// in the corresponding schema and then displays that schema and the definition @@ -946,15 +973,7 @@ namespace ICSharpCode.XmlEditor reader.WhitespaceHandling = WhitespaceHandling.None; StringWriter indentedXmlWriter = new StringWriter(); - XmlTextWriter writer = new XmlTextWriter(indentedXmlWriter); - if (xmlEditor.TextEditorProperties.ConvertTabsToSpaces) { - writer.Indentation = xmlEditor.TextEditorProperties.TabIndent; - writer.IndentChar = ' '; - } else { - writer.Indentation = 1; - writer.IndentChar = '\t'; - } - writer.Formatting = Formatting.Indented; + XmlTextWriter writer = CreateXmlTextWriter(indentedXmlWriter); writer.WriteNode(reader, false); writer.Flush(); @@ -966,6 +985,20 @@ namespace ICSharpCode.XmlEditor return indentedText; } + XmlTextWriter CreateXmlTextWriter(TextWriter textWriter) + { + XmlTextWriter writer = new XmlTextWriter(textWriter); + if (xmlEditor.TextEditorProperties.ConvertTabsToSpaces) { + writer.Indentation = xmlEditor.TextEditorProperties.TabIndent; + writer.IndentChar = ' '; + } else { + writer.Indentation = 1; + writer.IndentChar = '\t'; + } + writer.Formatting = Formatting.Indented; + return writer; + } + /// /// Checks that the xml in this view is well-formed. /// @@ -976,11 +1009,7 @@ namespace ICSharpCode.XmlEditor Document.LoadXml(Text); return true; } catch(XmlException ex) { - string fileName = FileName; - if (fileName == null || fileName.Length == 0) { - fileName = TitleName; - } - AddTask(fileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1, TaskType.Error); + AddTask(xmlEditor.FileName, ex.Message, ex.LinePosition - 1, ex.LineNumber - 1, TaskType.Error); } return false; }