为什么此属性未使用其命名空间进行序列化?

时间:2013-02-08 21:36:53

标签: c# xml-serialization xmlserializer

我有这两个类

[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都正确显示时,为什么version属性没有显示为xmi:version =“2.1”?

2 个答案:

答案 0 :(得分:1)

您显示的输出无法通过您显示的程序生成。

  1. schemaLocation属性的xsi名称空间不是“http://schema.omg.org/spec/XMI/2.1
  2. “架构位置字符串”不在程序中。
  3. 您需要发布已发布的程序生成的输出。

答案 1 :(得分:1)

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

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