2013-12-20 29 views
5

私は非常に奇妙な状況があります。C#XMLシリアル化:名前空間宣言の順序

:私は同じソフトウェアでこれを入手buildserverはオン

<SystemConfiguration 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns="http://www.schaeffler.com/sara/systemconfiguration/"> 

:私のマシン上で

var namespaces = new XmlSerializerNamespaces(); 
namespaces.Add("xsd", "http://www.w3.org/2001/XMLSchema"); 
namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance"); 

serializer.Serialize(writer, config, namespaces); 

私は、次のxml(1つのラインIだけ追加改行)を取得:私はこのように私の名前空間をシリアル化

<SystemConfiguration 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xmlns:xsd="http://www.w3.org/2001/XMLSchema" 
     xmlns="http://www.schaeffler.com/sara/systemconfiguration/"> 

xsdとxsiの順序が入れ替わっています。シリアライザの実装をチェックし、その順序がハッシュテーブルで決定され、XmlSerializerNamespacesがネームスペース用に独自のシリアライザを実装するためのインターフェイスがないことがわかりました。

これはXmlSerializationWriterにおける方法であって、ハッシュマップ内の名前空間の異なる順序を引き起こす可能性がありますどのような

protected void WriteNamespaceDeclarations(XmlSerializerNamespaces xmlns) 
    { 
     if (xmlns != null) 
     { 
     foreach (DictionaryEntry dictionaryEntry in xmlns.Namespaces) 
     { 
      string localName = (string) dictionaryEntry.Key; 
      string ns = (string) dictionaryEntry.Value; 
      if (this.namespaces != null) 
      { 
      string str = this.namespaces.Namespaces[(object) localName] as string; 
      if (str != null && str != ns) 
       throw new InvalidOperationException(Res.GetString("XmlDuplicateNs", (object) localName, (object) ns)); 
      } 
      string str1 = ns == null || ns.Length == 0 ? (string) null : this.Writer.LookupPrefix(ns); 
      if (str1 == null || str1 != localName) 
      this.WriteAttribute("xmlns", localName, (string) null, ns); 
     } 
     } 
     this.namespaces = (XmlSerializerNamespaces) null; 
    } 

? MSDNから

答えて

3

要素は、キーのハッシュ値に従ってソートされ、そして 各キーは、コレクションに一度しか存在することができます。

DictionaryEntryのハッシュ値(struct)はValueType.GetHashCode()から抽出されます。潜在的に基礎となる参照値に基づいて、決定不能なキーを返す可能性があります。特定のハッシュがどのように計算されているかを知るためには、さらなるリフレクションを行う必要があります。単にデフォルトのobject実装を使用している可能性があります。 MSDNからまた

は:

ハッシュコードは、ハッシュテーブルに基づいて コレクションに効率的に挿入し、ルックアップのために意図されています。ハッシュコードは ではありません。

関連する問題