Получить XmlRoot для класса - PullRequest
1 голос
/ 28 января 2011

Я бы хотел найти в XDocument вхождения SerializableClass внутри него.Когда SerializableClass имеет атрибут XmlRoot, он будет иметь имя, отличное от typeof(SerializableClass).Name.Как я могу найти атрибут XmlRoot этого класса?

1 Ответ

2 голосов
/ 28 января 2011

Вы имеете в виду, что хотите найти имя класса, который используется для представления определенного элемента в вашем 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;
            }
        }
...