XmlSerialization和xsi:SchemaLocation(xsd.exe)

Dav*_*yes 13 c# xsd xml-serialization xsd.exe gpx

我使用xsd.exe生成一个C#类来读/写GPX文件.如何获得生成的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>
Run Code Online (Sandbox Code Playgroud)

dtb*_*dtb 35

将其添加到生成的C#类:

[XmlAttribute("schemaLocation", Namespace = XmlSchema.InstanceNamespace)]
public string xsiSchemaLocation = "http://www.topografix.com/GPX/1/1 " +
                                  "http://www.topografix.com/GPX/1/1/gpx.xsd";
Run Code Online (Sandbox Code Playgroud)

显然,xsd.exe工具不生成schemaLocation属性.

  • 一个好主意是将它放在一个额外的文件中,并使用部分类来添加成员,这样就可以生成新类而不会丢失xsiSchemaLocation. (2认同)