Browse Source

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
shortcuts
Matt Ward 20 years ago
parent
commit
cc767e026e
  1. 28
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/CreateSchemaCommand.cs
  2. 36
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs

28
src/AddIns/DisplayBindings/XmlEditor/Project/Src/CreateSchemaCommand.cs

@ -32,11 +32,13 @@ namespace ICSharpCode.XmlEditor
XmlView xmlView = XmlView.ActiveXmlView; XmlView xmlView = XmlView.ActiveXmlView;
if (xmlView != null) { if (xmlView != null) {
// Create a schema based on the xml. // Create a schema based on the xml.
string schema = xmlView.CreateSchema(); string[] schemas = xmlView.InferSchema();
if (schema != null) { if (schemas != null) {
// Create a new file and display the generated schema. // Create a new file for each generated schema.
string fileName = GenerateSchemaFileName(xmlView.TextEditorControl.FileName); for (int i = 0; i < schemas.Length; ++i) {
OpenNewXmlFile(fileName, schema); string fileName = GenerateSchemaFileName(xmlView.TextEditorControl.FileName, i + 1);
OpenNewXmlFile(fileName, schemas[i]);
}
} }
} }
} }
@ -50,22 +52,16 @@ namespace ICSharpCode.XmlEditor
} }
/// <summary> /// <summary>
/// Generates an xsd filename based on the name of the original xml /// Generates an xsd filename based on the name of the original xml file.
/// file. If a file with the same name is already open in SharpDevelop
/// then a new name is generated (e.g. MyXml1.xsd).
/// </summary> /// </summary>
string GenerateSchemaFileName(string xmlFileName) string GenerateSchemaFileName(string xmlFileName, int count)
{ {
string baseFileName = Path.GetFileNameWithoutExtension(xmlFileName); string baseFileName = Path.GetFileNameWithoutExtension(xmlFileName);
string schemaFileName = String.Concat(baseFileName, ".xsd"); string schemaFileName = String.Concat(baseFileName, ".xsd");
if (count == 1) {
int count = 1; return schemaFileName;
while (FileService.IsOpen(schemaFileName)) {
schemaFileName = String.Concat(baseFileName, count.ToString(), ".xsd");
++count;
} }
return schemaFileName = String.Concat(baseFileName, count.ToString(), ".xsd");
return schemaFileName;
} }
} }
} }

36
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlView.cs

@ -377,23 +377,17 @@ namespace ICSharpCode.XmlEditor
/// <summary> /// <summary>
/// Creates a schema based on the xml content. /// Creates a schema based on the xml content.
/// </summary> /// </summary>
/// <returns>A generated schema or null if the xml content is not /// <returns>A set of generated schemas or null if the xml content is not
/// well formed.</returns> /// well formed.</returns>
public string CreateSchema() public string[] InferSchema()
{ {
TaskService.ClearExceptCommentTasks(); TaskService.ClearExceptCommentTasks();
if (IsWellFormed) { if (IsWellFormed) {
string schema; using (XmlTextReader reader = new XmlTextReader(new StringReader(Text))) {
using (DataSet dataSet = new DataSet()) { XmlSchemaInference schemaInference = new XmlSchemaInference();
dataSet.ReadXml(new StringReader(Text), XmlReadMode.InferSchema); XmlSchemaSet schemaSet = schemaInference.InferSchema(reader);
EncodedStringWriter writer = new EncodedStringWriter(xmlEditor.TextEditorProperties.Encoding); return GetSchemas(schemaSet);
XmlTextWriter xmlWriter = CreateXmlTextWriter(writer);
dataSet.WriteXmlSchema(xmlWriter);
schema = writer.ToString();
writer.Close();
xmlWriter.Close();
} }
return schema;
} else { } else {
ShowErrorList(); ShowErrorList();
} }
@ -1282,5 +1276,23 @@ namespace ICSharpCode.XmlEditor
} }
return null; return null;
} }
/// <summary>
/// Converts a set of schemas to a string array, each array item
/// contains the schema converted to a string.
/// </summary>
string[] GetSchemas(XmlSchemaSet schemaSet)
{
List<string> schemas = new List<string>();
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();
}
} }
} }

Loading…
Cancel
Save