简体   繁体   English

给定xml示例,自动生成xsd的最佳方法是什么?

[英]what's the best way to automate generation of an xsd given an xml sample?

I have a chunk of xml data that's coming out of a database that I need to generate an xsd for. 我有大量的xml数据从数据库中生成,我需要为其生成xsd。 Got it all working using xsd.exe but all the elements are showing up as string, even things like 2079.0200. 使用xsd.exe可以正常工作,但所有元素都显示为字符串,甚至是2079.0200之类的东西。 How do I get xsd.exe to guess at types? 如何获取xsd.exe来猜测类型? Would the XmlSchemaExporter class be able to do this? XmlSchemaExporter类可以做到这一点吗?

The issue here is that Visual Studio is generating the xsd that I want (with decimal types etc) when I use the XML --> Create Schema command, but I don't want to have to do this by hand. 这里的问题是,当我使用XML-> Create Schema命令时,Visual Studio会生成我想要的xsd(具有十进制类型等),但我不想手动执行此操作。 I'm setting up a process that takes in a chunk of xml and generates an XSD. 我正在设置一个过程,该过程接受大量xml并生成一个XSD。 But it needs to have more types than just "string". 但是它需要具有更多的类型,而不仅仅是“字符串”。

Related, but don't know if it's a solution yet (XmlSchemaInference class): Any tools to generate an XSD schema from an XML instance document? 相关,但不知道它是否是解决方案(XmlSchemaInference类):是否有任何工具可以从XML实例文档生成XSD模式?

The solution is to create the schema by hand, based on the one that's been generated. 解决方案是根据已生成的架构手动创建架构。 Then don't run XSD.EXE again. 然后,不要再次运行XSD.EXE。

John's answer is valid for situations where accuracy is more important than speed. John的答案适用于精度比速度更重要的情况。 For my situation, I needed many schemas that were identical to what would be produced via the VS "Create Schema" command. 对于我的情况,我需要许多与通过VS“创建模式”命令生成的模式相同的模式。 So accuracy wasn't as important as matching a known baseline and speed. 因此,准确性不如匹配已知的基线和速度那么重要。

This is what I ended up doing. 这就是我最终要做的。 It produced output identical to the VS "Create Schema" command: 它产生的输出与VS“ Create Schema”命令相同:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM