/// <include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.VerifyNCName"]/*' /> /// <devdoc> /// <para> /// </para> /// </devdoc> public static string VerifyNCName(string name) { if (name == null || name == string.Empty) { throw new ArgumentNullException("name"); } if (!XmlCharType.IsStartNCNameChar(name[0])) { throw new XmlException(Res.Xml_BadStartNameChar, XmlException.BuildCharExceptionStr(name[0])); } int nameLength = name.Length; int position = 1; while (position < nameLength) { if (!XmlCharType.IsNCNameChar(name[position])) { throw new XmlException(Res.Xml_BadNameChar, XmlException.BuildCharExceptionStr(name[position])); } position++; } return(name); }
private static string EncodeName(string name, bool first, bool local) { if (name == null) { return(name); } if (name == String.Empty) { if (!first) { throw new XmlException(Res.Xml_InvalidNmToken, name); } return(name); } StringBuilder bufBld = null; char[] source = name.ToCharArray(); int length = name.Length; int copyPosition = 0; int position = 0; int underscorePos = name.IndexOf('_'); MatchCollection mc = null; IEnumerator en = null; if (underscorePos >= 0) { mc = c_EncodeCharPattern.Matches(name, underscorePos); en = mc.GetEnumerator(); } int matchPos = -1; if (en != null && en.MoveNext()) { Match m = (Match)en.Current; matchPos = m.Index - 1; } if (first) { if ((local && !XmlCharType.IsStartNCNameChar(source[0])) || (!local && !XmlCharType.IsStartNameChar(source[0])) || matchPos == 0) { if (bufBld == null) { bufBld = new StringBuilder(length + 20); } bufBld.Append("_x"); if (length > 1 && source[0] >= 0xd800 && source[0] <= 0xdbff && source[1] >= 0xdc00 && source[1] <= 0xdfff) { int x = source[0]; int y = source[1]; Int32 u = (x - 0xD800) * 0x400 + (y - 0xDC00) + 0x10000; bufBld.Append(u.ToString("X8")); position++; copyPosition = 2; } else { bufBld.Append(((Int32)source[0]).ToString("X4")); copyPosition = 1; } bufBld.Append("_"); position++; if (matchPos == 0) { if (en.MoveNext()) { Match m = (Match)en.Current; matchPos = m.Index - 1; } } } } for (; position < length; position++) { if ((local && !XmlCharType.IsNCNameChar(source[position])) || (!local && !XmlCharType.IsNameChar(source[position])) || (matchPos == position)) { if (bufBld == null) { bufBld = new StringBuilder(length + 20); } if (matchPos == position) { if (en.MoveNext()) { Match m = (Match)en.Current; matchPos = m.Index - 1; } } bufBld.Append(source, copyPosition, position - copyPosition); bufBld.Append("_x"); if ((length > position + 1) && source[position] >= 0xd800 && source[position] <= 0xdbff && source[position + 1] >= 0xdc00 && source[position + 1] <= 0xdfff) { int x = source[position]; int y = source[position + 1]; Int32 u = (x - 0xD800) * 0x400 + (y - 0xDC00) + 0x10000; bufBld.Append(u.ToString("X8")); copyPosition = position + 2; position++; } else { bufBld.Append(((Int32)source[position]).ToString("X4")); copyPosition = position + 1; } bufBld.Append("_"); } } if (copyPosition == 0) { return(name); } else { if (copyPosition < length) { bufBld.Append(source, copyPosition, length - copyPosition); } return(bufBld.ToString()); } }