XmlSerialization и xsi:SchemaLocation (xsd.exe)

"Функции More NIO" имеют функциональность часов файла с зависящим от реализации на базовую ОС. Должен быть в JDK7.

13
задан John Saunders 11 September 2009 в 00:06
поделиться

2 ответа

Add this to your generated C# class:

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

Apparently the xsd.exe tool does not generate the schemaLocation attribute.

35
ответ дан 1 December 2019 в 18:49
поделиться

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:

[XmlRoot(ElementName = "gpx", Namespace = GPX_NAMESPACE)]
public class WhateverAGpxIs
{
    private const string GPX_NAMESPACE = "http://www.topografix.com/GPX/1/1";

    private const string XSI_NAMESPACE =
        "http://www.w3.org/2001/XMLSchema-instance";

    [XmlAttribute(AttributeName = "creator")]
    public string Creator = "ExpertGPS 1.1 - http://www.topografix.com";

    [XmlNamespaceDeclarations]
    public XmlSerializerNamespaces Namespaces =
        new XmlSerializerNamespaces(
            new[]
                {
                    new XmlQualifiedName("xsi", XSI_NAMESPACE),
                    new XmlQualifiedName(string.Empty, GPX_NAMESPACE)
                });

    [XmlAttribute(AttributeName = "schemaLocation",
        Namespace = XSI_NAMESPACE)]
    public string SchemaLocation = GPX_NAMESPACE + " " +
                                   "http://www.topografix.com/GPX/1/1/gpx.xsd";

    [XmlAttribute(AttributeName = "version")]
    public string Version = "1.1";
}
2
ответ дан 1 December 2019 в 18:49
поделиться
Другие вопросы по тегам:

Похожие вопросы: