の.Net 4.5は、スキーマの推論を持っている...
https://msdn.microsoft.com/en-us/library/xz2797k1(v=vs.110).aspx
これは複数のソースを受け入れることができます!
私はこのコードを書いたので、複数のファイルパスを渡すこともできます。最初のファイルパスは、書き込むxsdファイルであり、後続のファイルは入力Xmlファイルです。これはコンソールアプリケーションです。
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
namespace SchemaInferrer
{
class Program
{
static void Main(string[] args)
{
string xsdFile="";
string[] xmlFiles=null;
DivideArguments(args, ref xsdFile, ref xmlFiles);
if (FilesExist(xmlFiles))
{
Console.WriteLine("All files exist, good to infer...");
XmlSchemaSet schemaSet = new XmlSchemaSet();
XmlSchemaInference inference = new XmlSchemaInference();
bool bFirstTime = true;
foreach (string sFile in xmlFiles)
{
XmlReader reader = XmlReader.Create(sFile);
if (bFirstTime)
{
schemaSet = inference.InferSchema(reader);
} else
{
schemaSet = inference.InferSchema(reader, schemaSet);
}
bFirstTime = false;
}
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings()
{
Indent = true,
IndentChars = "\t"
};
XmlWriter writer = XmlWriter.Create(xsdFile, xmlWriterSettings);
foreach (XmlSchema schema in schemaSet.Schemas())
{
//schema.Write(Console.Out);
schema.Write(writer);
}
Console.WriteLine("Finished, wrote file to {0}...",xsdFile);
//Console.ReadLine();
}
}
static void DivideArguments(string [] args, ref string xsdFile, ref string[] xmlFiles)
{
xsdFile = args[0];
xmlFiles=new string[args.Length-1];
for (int i = 0; i < args.Length-1; i++)
{
xmlFiles[i] = args[i + 1];
}
}
static bool FilesExist(string[] args)
{
bool bFilesExist=true; //* until proven otherwise
if (args.Length>0)
{
foreach (string sFile in args)
{
if (!File.Exists(sFile))
bFilesExist=false;
}
}
return bFilesExist;
}
}
}
TrangのホームページはまだGoogleコードにリンクしていますが、プロジェクトはGithubに移動しました。将来それを見つけた他の人にとって、https://github.com/relaxng/jing-trangはTrangレポの新しい場所です。 – rmunn
@ryanStull:例を挙げることはできますか?私はちょうど4つのxmlファイルの慎重に構築されたセットからTrangを使用してxsdを作ってきました。もう1つはfreeformatter.comを1つのファイルから使用しています。彼らは非常に異なっており、私は自分自身のバリデーションを追加するためにどの時間を使うべきかを決めるまでにはしばらく時間がかかるでしょう。 –