Programmatically Create XML File From XSD

非 Y 不嫁゛ 提交于 2020-01-01 17:06:49

问题


I have an XSD file and I want to create an XML file at runtime based on the XSD. (My question is basically the same as this one, except in the .NET world, rather than for Java.)

I have run xsd.exe /c /l:vb MyTest.xsd, but that generates a class that cannot be instantiated and used, as it has nested classes that are never instantiated. It appears that the generated class is only used for XML deserialization, but I don't have an XML file to deserialize from.

Running xsd.exe /d /l:vb MyTest.xsd generates a dataset, but it looks like using that would be more coding than just writing out an XmlDocument manually.

What is the best way to create an XML file based on a specific schema?


回答1:


Read this article: http://msdn.microsoft.com/en-us/library/aa302296.aspx it includes a installer and samples on using the XmlSampleGenerator API.




回答2:


It turns out I was just being obtuse. While the classes generated by xsd contain uninitialized nested classes, they are exposed via public properties, so there is nothing stopping me from instantiating them myself.

Something like the following:

ElementA.ElementB = New ElementB()
ElementA.ElementB.Name = "Jason"



回答3:


I use an XML tool (oXygen) that offers this as part of its capabilities. I'm sure there are others, that's just what we use here.



来源:https://stackoverflow.com/questions/2140828/programmatically-create-xml-file-from-xsd

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!