XmlSerialization 和 xsi:SchemaLocation (xsd.exe)
我使用 xsd.exe 生成用于读取/写入 GPX 文件的 C# 类。如何使生成的 XML 文件包含 xsi:schemaLocation 属性 例如。
我想要以下内容,但 xsi:schemaLocation 始终丢失
<?xml version="1.0"?>
<gpx
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
version="1.1"
xmlns="http://www.topografix.com/GPX/1/1"
creator="ExpertGPS 1.1 - http://www.topografix.com"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
</gpx>
I used xsd.exe to generate a C# class for reading/writing GPX files. How do I get the resultant XML file to include the xsi:schemaLocation attribute
eg.
I want the following but xsi:schemaLocation is always missing
<?xml version="1.0"?>
<gpx
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
version="1.1"
xmlns="http://www.topografix.com/GPX/1/1"
creator="ExpertGPS 1.1 - http://www.topografix.com"
xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd">
</gpx>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
将其添加到生成的 C# 类中:
显然
xsd.exe
工具不会生成schemaLocation
属性。Add this to your generated C# class:
Apparently the
xsd.exe
tool does not generate theschemaLocation
attribute.您可以扩展该类并将其添加到扩展类中,而不是修改 xsd.exe 生成的类来添加 schemaLocation 属性。
假设原始架构名为 MySchema.xsd,生成的文件名为 MySchema.cs,类名为 MySchema。生成的类可能如下所示:
[MySchema.cs]
(请注意,该类是部分的。这意味着我们可以扩展它。)
您需要做的是创建另一个文件,在本例中我们将其称为 MySchemaExtender。 CS。该文件将包含具有相同类名 MySchema 的另一个部分类定义:
[MySchemaExtender.cs]
现在您需要做的就是将 schemaLocation 属性放入扩展类中。最终的扩展类如下所示:
[MySchemaExtender.cs]
现在,如果您使用 xsd.exe 重新生成该类,则无需修改任何内容。
Instead of modifying the class generated by xsd.exe to add the schemaLocation attribute you can extend the class and add it in your extended class.
Lets say the original schema is called MySchema.xsd and the generated file name is MySchema.cs and the class name is MySchema. Here is what the generated class might look like:
[MySchema.cs]
(Note that the class is partial. This means we can extend it.)
What you need to do is create another file, in this example we will call it MySchemaExtender.cs. This file will contain another partial class definition with the same class name MySchema:
[MySchemaExtender.cs]
Now all you need to do is put the schemaLocation attribute in the extended class. Here is what your final extended class will look like:
[MySchemaExtender.cs]
Now if you regenerate the class using xsd.exe you won't have to modify anything.
您必须自己完成此操作。在任何情况下,XML 序列化都无法知道您想要架构的位置。
试试这个,虽然我还没有测试过:
You'll have to do this on your own. There's no way for XML Serialization to know where you want your schema to go in any case.
Try this, though I haven't tested it yet:
当然这个答案已经太晚了!但也许对其他开发人员有用;-)。我使用反射来解决这个问题,因为它必须是自动化的。
必须调用静态方法 CreateMessageType。必须是不包含 schemaLocation 属性的序列化类。此方法使用作为父级(名为 Dynamic)返回新类型,但添加 schemaLocation 属性并将 ElementName 属性设置为 XmlRootAttribute。
创建类型后,必须再次使用反射来创建对象并设置属性。
这段代码在 xxx 中看起来相当痛苦,但它的工作原理却很有魅力!
请参阅下面的编码:
我使用以下代码来创建对象
最终得到完美的输出:
Of course this answer is way too late! But maybe usefull for other developers ;-). I used relfection for solving this problem, because it had to be automated.
The static method CreateMessageType must be called. must be the serialized class NOT containing the schemaLocation property. This method return a new type using the as parent (named Dynamic), but add the schemaLocation properties and sets ElementName property to of the XmlRootAttribute.
After creating the type, reflection must again be used to create the object and set the properties.
The code looks like quite a pain in the xxx, but it works like a charm!
See the coding below:
The following code I used to create the object
And eventually the perfect output: