2011-01-28 5 views
1

XDocument内に出現頻度がSerializableClassであることを検索したいと思います。 SerializableClassにXmlRoot属性がある場合、typeof(SerializableClass).Nameとは異なる名前になります。このクラスのXmlRoot属性を検索するにはどうすればよいですか?クラスのXmlRootを取得する

答えて

2

XMLの特定の要素を表すために使用されるクラスの名前を検索することを意味しますか?

これは、私はそれを行う方法です。

/// <summary> 
     /// Determines and returns the name of the XmlElement that should represent instances of the given type 
     /// in an XML stream. 
     /// </summary> 
     /// <param name="serializedObjectType"></param> 
     /// <returns></returns>   
     [SuppressMessage ("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters")] 
     public static string GetRootNodeElementNameForType(Type serializedObjectType) 
     { 
      // Determine if the Type contains an XmlRoot Attribute. If so, the XmlRoot attribute should contain 
      // the name of the element-name for this type. 
      // Otherwise, the name of the type should 've been used for serializing objects of this type. 
      XmlRootAttribute theAttrib = Attribute.GetCustomAttribute (serializedObjectType, typeof (XmlRootAttribute)) as XmlRootAttribute; 

      if(theAttrib != null) 
      { 
       if(String.IsNullOrEmpty (theAttrib.ElementName) == false) 
       { 
        return theAttrib.ElementName; 
       } 
       else 
       { 
        return serializedObjectType.Name; 
       } 
      } 
      else 
      { 
       return serializedObjectType.Name; 
      } 
     } 
+1

== falseの?怒り! :D –

+0

あなたの時計が何であれ間違いありません。 :) –

関連する問題