以编程方式创建XML文件从XSD方式、文件、XML、XSD

2023-09-03 15:14:28 作者:酒场↘浪人

我有一个XSD文件,我想在运行时创建的基础上XSD的XML文件。 (我的问题是基本相同这一个,除了在.NET的世界,而不是Java的。)

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.)

我已经运行 XSD.EXE / C / L:VB MyTest.xsd ,但生成不能被实例化的一类,因为它嵌套类从来没有实例化。看来,生成的类仅用于XML反序列化,但我没有一个XML文件,从反序列化。

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.

运行 XSD.EXE / D / L:VB MyTest.xsd 生成一个数据集,但它看起来像使用会更编码不仅仅是编写了一个的XmlDocument 手动。

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.

什么是创建一个基于特定架构的XML文件的最佳方法是什么?

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

推荐答案

原来我只是被钝。虽然由XSD生成的类包含未初始化的嵌套类,它们通过公共财产暴露,因此没有任何实例化他们自己阻止我。

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.

类似如下:

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