简体   繁体   中英

Why is this attribute not serialized with its namespace?

I have these two classes

[XmlRoot(ElementName="XMI", Namespace = XmiStrings.XmiNamespace)]
public class XMIElement
{
    [XmlAttribute("version", Namespace = XmiStrings.XmiNamespace)]
    public string Version { get; set; }
    [XmlAttribute("schemaLocation", Namespace = XmiStrings.XsiNamespace)]
    public string SchemaLocation { get; set; }

}

public static class XmiStrings
{
    public const string XmiNamespace = "http://schema.omg.org/spec/XMI/2.1";
    public const string XsiNamespace = "http://www.w3.org/2001/XMLSchema-instance";
    public const string PackageTypesNamespace = "http://SysID_Profile.ServiceProfile/schemas/PackageTypes/_zdnHsHCBEeKdD9Oad6OUOQ/0";
    public const string RHPNamespace = "http://RhapsodyStandardModel.RhpProperties/schemas/RHP/_zddWsXCBEeKdD9Oad6OUOQ/0";
    public const string RhapsodyProfileNamespace = "http://RhapsodyStandardModel/schemas/RhapsodyProfile/_zddWsHCBEeKdD9Oad6OUOQ/0";
    public const string ServiceProfileNamespace = "http://SysID_Profile/schemas/ServiceProfile/_zdnHsXCBEeKdD9Oad6OUOQ/0";
    public const string SimpleTypesNamespace = "http://SysID_Profile.ServiceProfile/schemas/SimpleTypes/_zddWsnCBEeKdD9Oad6OUOQ/0";
    public const string StandardNamespace = "http://schema.omg.org/spec/UML/2.1/StandardProfileL2";
    public const string EcoreNamespace = "http://www.eclipse.org/emf/2002/Ecore";
    public const string UmlNamespace = "http://schema.omg.org/spec/UML/2.1";
    public const string SchemaLocation = "http://SysID_Profile.ServiceProfile/schemas/PackageTypes/_zdnHsHCBEeKdD9Oad6OUOQ/0 #_zpg3YHCBEeKdD9Oad6OUOQ http://RhapsodyStandardModel.RhpProperties/schemas/RHP/_zddWsXCBEeKdD9Oad6OUOQ/0 #_zpqBA3CBEeKdD9Oad6OUOQ http://RhapsodyStandardModel/schemas/RhapsodyProfile/_zddWsHCBEeKdD9Oad6OUOQ/0 #_zpp_o3CBEeKdD9Oad6OUOQ http://SysID_Profile/schemas/ServiceProfile/_zdnHsXCBEeKdD9Oad6OUOQ/0 #_zpg1_nCBEeKdD9Oad6OUOQ http://SysID_Profile.ServiceProfile/schemas/SimpleTypes/_zddWsnCBEeKdD9Oad6OUOQ/0 #_zpg2fXCBEeKdD9Oad6OUOQ http://schema.omg.org/spec/UML/2.1/StandardProfileL2 http://schema.omg.org/spec/UML/2.1/StandardProfileL2.xmi#_yzU58YinEdqtvbnfB2L_5w http://schema.omg.org/spec/UML/2.1 http://www.eclipse.org/uml2/2.0.0/UML";
}

And this Console application

class Program
{
     static void Main(string[] args)
    {
        var xmi = new XMIElement();
        xmi.Version = "2.1";
        xmi.SchemaLocation = XmiStrings.SchemaLocation;

        var serializer = new XmlSerializer(typeof(XMIElement));
        var writer = new StreamWriter("test.xml");
        serializer.Serialize(writer, xmi, GetNamespaces());
    }

    private static XmlSerializerNamespaces GetNamespaces()
    {
        var ns = new XmlSerializerNamespaces();
        ns.Add("xmi", XmiStrings.XmiNamespace);
        ns.Add("xsi", XmiStrings.XsiNamespace);
        ns.Add("PackageTypes", XmiStrings.PackageTypesNamespace);
        ns.Add("RHP", XmiStrings.RHPNamespace);
        ns.Add("RhapsodyProfile", XmiStrings.RhapsodyProfileNamespace);
        ns.Add("ServiceProfile", XmiStrings.ServiceProfileNamespace);
        ns.Add("SimpleTypes", XmiStrings.SimpleTypesNamespace);
        ns.Add("Standard", XmiStrings.StandardNamespace);
        ns.Add("ecore", XmiStrings.EcoreNamespace);
        ns.Add("uml", XmiStrings.UmlNamespace);
        return ns;
    }
}

which gives this output

 <?xml version="1.0" encoding="utf-8"?>
 <xmi:XMI xmlns:PackageTypes="http://SysID_Profile.ServiceProfile/schemas/PackageTypes/_zdnHsHCBEeKdD9Oad6OUOQ/0"
     xmlns:RHP="http://RhapsodyStandardModel.RhpProperties/schemas/RHP/_zddWsXCBEeKdD9Oad6OUOQ/0"
     xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore"
     xmlns:ServiceProfile="http://SysID_Profile/schemas/ServiceProfile/_zdnHsXCBEeKdD9Oad6OUOQ/0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xmlns:RhapsodyProfile="http://RhapsodyStandardModel/schemas/RhapsodyProfile/_zddWsHCBEeKdD9Oad6OUOQ/0"
     xmlns:SimpleTypes="http://SysID_Profile.ServiceProfile/schemas/SimpleTypes/_zddWsnCBEeKdD9Oad6OUOQ/0"
     xmlns:Standard="http://schema.omg.org/spec/UML/2.1/StandardProfileL2"
     xmlns:uml="http://schema.omg.org/spec/UML/2.1"
     version="2.1"
     xsi:schemaLocation="http://SysID_Profile.ServiceProfile/schemas/PackageTypes/_zdnHsHCBEeKdD9Oad6OUOQ/0 #_zpg3YHCBEeKdD9Oad6OUOQ http://RhapsodyStandardModel.RhpProperties/schemas/RHP/_zddWsXCBEeKdD9Oad6OUOQ/0 #_zpqBA3CBEeKdD9Oad6OUOQ http://RhapsodyStandardModel/schemas/RhapsodyProfile/_zddWsHCBEeKdD9Oad6OUOQ/0 #_zpp_o3CBEeKdD9Oad6OUOQ http://SysID_Profile/schemas/ServiceProfile/_zdnHsXCBEeKdD9Oad6OUOQ/0 #_zpg1_nCBEeKdD9Oad6OUOQ http://SysID_Profile.ServiceProfile/schemas/SimpleTypes/_zddWsnCBEeKdD9Oad6OUOQ/0 #_zpg2fXCBEeKdD9Oad6OUOQ http://schema.omg.org/spec/UML/2.1/StandardProfileL2 http://schema.omg.org/spec/UML/2.1/StandardProfileL2.xmi#_yzU58YinEdqtvbnfB2L_5w http://schema.omg.org/spec/UML/2.1 http://www.eclipse.org/uml2/2.0.0/UML"
     xmlns:xmi="http://schema.omg.org/spec/XMI/2.1" />

so my question is: Why doesn't the version attribute have show as xmi:version="2.1" when both the XMI element and the SchemaLocation show correctly?

The output you are showing cannot be produced from the program you showed.

  1. schemaLocation attribute has xsi namespace not " http://schema.omg.org/spec/XMI/2.1 "
  2. "schema location string" is not in the program.

You need to post the output generated by the program posted.

You need to add Form to the attribute:

[XmlAttribute("version", Namespace = XmiStrings.XmiNamespace, Form=System.Xml.Schema.XmlSchemaForm.Qualified)] 
public string Version { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM