YAXLib.StringUtils.RefineSingleElement C# (CSharp) Method

RefineSingleElement() public static method

Refines a single element name. Refines the location string. Trims it, and replaces invlalid characters with underscore.
public static RefineSingleElement ( string elemName ) : string
elemName string Name of the element.
return string
        public static string RefineSingleElement(string elemName)
        {
            if (elemName == null)
                return null;

            elemName = elemName.Trim(' ', '\t', '\r', '\n', '\v', '/', '\\');
            if (IsSingleLocationGeneric(elemName))
            {
                return elemName;
            }
            else if (LooksLikeExpandedXName(elemName))
            {
                // thanks go to CodePlex user: tg73 (http://www.codeplex.com/site/users/view/tg73)
                // for providing the code for expanded xml name support

                // XName permits strings of the form '{namespace}localname'. Detecting such cases allows
                // YAXLib to support explicit namespace use.
                // http://msdn.microsoft.com/en-us/library/system.xml.linq.xname.aspx

                // Leave namespace part alone, refine localname part.
                int closingBrace = elemName.IndexOf( '}' );
                string refinedLocalname = RefineSingleElement( elemName.Substring( closingBrace + 1 ) );
                return elemName.Substring( 0, closingBrace + 1 ) + refinedLocalname;
            }
            else
            {
                var sb = new StringBuilder(elemName.Length);

                // This uses the rules defined in http://www.w3.org/TR/xml/#NT-Name.
                // Thanks go to [@asbjornu] for pointing to the W3C standard
                for (int i = 0; i < elemName.Length; i++)
                {
                    if (i == 0)
                        sb.Append(IsValidNameStartChar(elemName[i]) ? elemName[i] : '_');
                    else
                        sb.Append(IsValidNameChar(elemName[i]) ? elemName[i] : '_');
                }

                return sb.ToString();
            }
        }

Usage Example

Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MemberWrapper"/> class.
        /// </summary>
        /// <param name="memberInfo">The member-info to build this instance from.</param>
        /// <param name="callerSerializer">The caller serializer.</param>
        public MemberWrapper(MemberInfo memberInfo, YAXSerializer callerSerializer)
        {
            if (!(memberInfo.MemberType == MemberTypes.Property || memberInfo.MemberType == MemberTypes.Field))
            {
                throw new Exception("Member must be either property or field");
            }

            m_memberInfo = memberInfo;
            m_isProperty = (memberInfo.MemberType == MemberTypes.Property);

            Alias = StringUtils.RefineSingleElement(m_memberInfo.Name);

            if (m_isProperty)
            {
                m_propertyInfoInstance = (PropertyInfo)memberInfo;
            }
            else
            {
                m_fieldInfoInstance = (FieldInfo)memberInfo;
            }

            m_memberType = m_isProperty ? m_propertyInfoInstance.PropertyType : m_fieldInfoInstance.FieldType;

            m_memberTypeWrapper = TypeWrappersPool.Pool.GetTypeWrapper(MemberType, callerSerializer);

            InitInstance();

            TreatErrorsAs = callerSerializer != null ? callerSerializer.DefaultExceptionType : YAXExceptionTypes.Error;

            // discovver YAXCustomSerializerAttributes earlier, because some other attributes depend on it
            var attrsToProcessEarlier = new HashSet <Type> {
                typeof(YAXCustomSerializerAttribute), typeof(YAXCollectionAttribute)
            };

            foreach (var attrType in attrsToProcessEarlier)
            {
                var customSerAttrs = m_memberInfo.GetCustomAttributes(attrType, true);
                foreach (var attr in customSerAttrs)
                {
                    ProcessYaxAttribute(attr);
                }
            }

            foreach (var attr in m_memberInfo.GetCustomAttributes(false))
            {
                // no need to preces, it has been proccessed earlier
                if (attrsToProcessEarlier.Contains(attr.GetType()))
                {
                    continue;
                }
                //if (attr is YAXCustomSerializerAttribute)
                //    continue; // no need to preces, it has been proccessed earlier

                if (attr is YAXBaseAttribute)
                {
                    ProcessYaxAttribute(attr);
                }
            }
        }
All Usage Examples Of YAXLib.StringUtils::RefineSingleElement