2009-12-30 57 views
0

我有一个xml数据块出来,我需要生成一个xsd的数据库。所有这些工作都使用xsd.exe,但所有元素都显示为字符串,甚至像2079.0200这样的东西。如何让xsd.exe猜测类型? XmlSchemaExporter类能够做到这一点吗?给xml样本自动生成xsd的最佳方式是什么?

这里的问题是,当我使用XML - > Create Schema命令时,Visual Studio生成了我想要的xsd(带有十进制类型等),但我不想手动执行此操作。我正在设置一个进程,该进程需要一大块xml并生成一个XSD。但它需要的类型不仅仅是“字符串”。

相关,但不知道这是否是一个解决方案,但(XmlSchemaInference类):Any tools to generate an XSD schema from an XML instance document?

回答

0

John的答案适用于精度比速度更重要的情况。对于我的情况,我需要许多与通过VS“Create Schema”命令生成的模式相同的模式。所以精确度不如匹配已知的基线和速度那么重要。

这就是我最终做的。它产生的输出等同于VS“创建模式”命令:

XmlSchemaInference inf = new XmlSchemaInference(); 

// xml variable on the next line is a string being passed in 
XmlSchemaSet schemas = inf.InferSchema(new XmlTextReader(xml, XmlNodeType.Element, null)); 
schemas.Compile(); 

XmlSchema[] schemaArray = new XmlSchema[1]; 
schemas.CopyTo(schemaArray, 0); 
XmlTextWriter wr = new XmlTextWriter(xsdOutputFileNameAndPath, Encoding.UTF8); 
wr.Formatting = Formatting.Indented; 
schemaArray[0].Write(wr); 
wr.Close(); 
0

的解决方案是手动创建架构,基于一个已经产生的一个。然后不要再次运行XSD.EXE。

+0

Visual Studio的管理,以基于XML的样本,我想的模式,因此“手做”是不是一个很好的答案。我想知道我是否可以连接到Visual Studio ... – jcollum 2009-12-30 02:49:07

+0

Visual Studio正在调用一个猜测模式的.NET API。你不必猜测 - 你知道模式应该是什么。所以用你的知识来修正模式是正确的。请注意,XSD.EXE/Visual Studio也无法猜测元素是否为必需元素,或者发生了什么限制,或者继承结构或缺少的属性等。您不能依赖于推断的模式。 – 2009-12-30 03:04:55

+0

我有40多个这些产生,他们不必是超级准确的。它看起来像XmlSchemaInference会做我所需要的,或者至少这是我要去的方向。 – jcollum 2009-12-30 03:07:47

相关问题