0

我有这两个课

[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";
}

而这个控制台应用程序

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;
    }
}

这给出了这个输出

 <?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" />

所以我的问题是:当 XMI 元素和 SchemaLocation 都正确显示时,为什么版本属性没有显示为 xmi:version="2.1" ?

4

2 回答 2

1

您显示的输出无法从您显示的程序中产生。

  1. schemaLocation 属性具有 xsi 命名空间而不是“ http://schema.omg.org/spec/XMI/2.1
  2. “模式位置字符串”不在程序中。

您需要发布发布的程序生成的输出。

于 2013-02-08T21:47:35.723 回答
1

您需要将 Form 添加到属性中:

[XmlAttribute("version", Namespace = XmiStrings.XmiNamespace, Form=System.Xml.Schema.XmlSchemaForm.Qualified)] 
public string Version { get; set; }
于 2014-02-17T12:23:53.437 回答