XmlConvert.cs 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689
  1. //------------------------------------------------------------------------------
  2. // <copyright file="XmlConvert.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. // <owner current="true" primary="true">Microsoft</owner>
  6. //------------------------------------------------------------------------------
  7. namespace System.Xml {
  8. using System.IO;
  9. using System.Text;
  10. using System.Globalization;
  11. using System.Xml.Schema;
  12. using System.Diagnostics;
  13. using System.Collections;
  14. using System.Text.RegularExpressions;
  15. // ExceptionType enum is used inside XmlConvert to specify which type of exception should be thrown at some of the verification and exception creating methods
  16. internal enum ExceptionType {
  17. ArgumentException,
  18. XmlException,
  19. }
  20. // Options for serializing and deserializing DateTime
  21. public enum XmlDateTimeSerializationMode {
  22. Local,
  23. Utc,
  24. Unspecified,
  25. RoundtripKind,
  26. }
  27. /// <include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert"]/*' />
  28. /// <devdoc>
  29. /// Encodes and decodes XML names according to
  30. /// the "Encoding of arbitrary Unicode Characters in XML Names" specification.
  31. /// </devdoc>
  32. public class XmlConvert {
  33. //
  34. // Static fields with implicit initialization
  35. //
  36. static XmlCharType xmlCharType = XmlCharType.Instance;
  37. #if !SILVERLIGHT
  38. internal static char[] crt = new char[] {'\n', '\r', '\t'};
  39. #endif
  40. /// <include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.EncodeName"]/*' />
  41. /// <devdoc>
  42. /// <para>
  43. /// Converts names, such
  44. /// as DataTable or
  45. /// DataColumn names, that contain characters that are not permitted in
  46. /// XML names to valid names.</para>
  47. /// </devdoc>
  48. public static string EncodeName(string name) {
  49. return EncodeName(name, true/*Name_not_NmToken*/, false/*Local?*/);
  50. }
  51. /// <include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.EncodeNmToken"]/*' />
  52. /// <devdoc>
  53. /// <para> Verifies the name is valid
  54. /// according to production [7] in the XML spec.</para>
  55. /// </devdoc>
  56. public static string EncodeNmToken(string name) {
  57. return EncodeName(name, false/*Name_not_NmToken*/, false/*Local?*/);
  58. }
  59. /// <include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.EncodeLocalName"]/*' />
  60. /// <devdoc>
  61. /// <para>Converts names, such as DataTable or DataColumn names, that contain
  62. /// characters that are not permitted in XML names to valid names.</para>
  63. /// </devdoc>
  64. public static string EncodeLocalName(string name) {
  65. return EncodeName(name, true/*Name_not_NmToken*/, true/*Local?*/);
  66. }
  67. /// <include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.DecodeName"]/*' />
  68. /// <devdoc>
  69. /// <para>
  70. /// Transforms an XML name into an object name (such as DataTable or DataColumn).</para>
  71. /// </devdoc>
  72. public static string DecodeName(string name) {
  73. if (name == null || name.Length == 0)
  74. return name;
  75. StringBuilder bufBld = null;
  76. int length = name.Length;
  77. int copyPosition = 0;
  78. int underscorePos = name.IndexOf('_');
  79. MatchCollection mc = null;
  80. IEnumerator en = null;
  81. if (underscorePos >= 0)
  82. {
  83. if ( c_DecodeCharPattern == null ) {
  84. c_DecodeCharPattern = new Regex("_[Xx]([0-9a-fA-F]{4}|[0-9a-fA-F]{8})_");
  85. }
  86. mc = c_DecodeCharPattern.Matches(name, underscorePos);
  87. en = mc.GetEnumerator();
  88. } else {
  89. return name;
  90. }
  91. int matchPos = -1;
  92. if (en != null && en.MoveNext())
  93. {
  94. Match m = (Match)en.Current;
  95. matchPos = m.Index;
  96. }
  97. for (int position = 0; position < length - c_EncodedCharLength + 1; position ++) {
  98. if (position == matchPos) {
  99. if (en.MoveNext())
  100. {
  101. Match m = (Match)en.Current;
  102. matchPos = m.Index;
  103. }
  104. if (bufBld == null) {
  105. bufBld = new StringBuilder(length + 20);
  106. }
  107. bufBld.Append(name, copyPosition, position - copyPosition);
  108. if (name[position + 6]!='_') { //_x1234_
  109. Int32 u =
  110. FromHex(name[position + 2]) * 0x10000000 +
  111. FromHex(name[position + 3]) * 0x1000000 +
  112. FromHex(name[position + 4]) * 0x100000 +
  113. FromHex(name[position + 5]) * 0x10000 +
  114. FromHex(name[position + 6]) * 0x1000 +
  115. FromHex(name[position + 7]) * 0x100 +
  116. FromHex(name[position + 8]) * 0x10 +
  117. FromHex(name[position + 9]);
  118. if (u >= 0x00010000) {
  119. if (u <= 0x0010ffff) { //convert to two chars
  120. copyPosition = position + c_EncodedCharLength + 4;
  121. char lowChar, highChar;
  122. XmlCharType.SplitSurrogateChar(u, out lowChar, out highChar);
  123. bufBld.Append(highChar);
  124. bufBld.Append(lowChar);
  125. }
  126. //else bad ucs-4 char dont convert
  127. }
  128. else { //convert to single char
  129. copyPosition = position + c_EncodedCharLength + 4;
  130. bufBld.Append((char)u);
  131. }
  132. position += c_EncodedCharLength - 1 + 4; //just skip
  133. }
  134. else {
  135. copyPosition = position + c_EncodedCharLength;
  136. bufBld.Append((char)(
  137. FromHex(name[position + 2]) * 0x1000 +
  138. FromHex(name[position + 3]) * 0x100 +
  139. FromHex(name[position + 4]) * 0x10 +
  140. FromHex(name[position + 5])));
  141. position += c_EncodedCharLength - 1;
  142. }
  143. }
  144. }
  145. if (copyPosition == 0) {
  146. return name;
  147. }
  148. else {
  149. if (copyPosition < length) {
  150. bufBld.Append(name, copyPosition, length - copyPosition);
  151. }
  152. return bufBld.ToString();
  153. }
  154. }
  155. private static string EncodeName(string name, /*Name_not_NmToken*/ bool first, bool local) {
  156. if (string.IsNullOrEmpty(name)) {
  157. return name;
  158. }
  159. StringBuilder bufBld = null;
  160. int length = name.Length;
  161. int copyPosition = 0;
  162. int position = 0;
  163. int underscorePos = name.IndexOf('_');
  164. MatchCollection mc = null;
  165. IEnumerator en = null;
  166. if (underscorePos >= 0)
  167. {
  168. if ( c_EncodeCharPattern == null ) {
  169. c_EncodeCharPattern = new Regex("(?<=_)[Xx]([0-9a-fA-F]{4}|[0-9a-fA-F]{8})_");
  170. }
  171. mc = c_EncodeCharPattern.Matches(name, underscorePos);
  172. en = mc.GetEnumerator();
  173. }
  174. int matchPos = -1;
  175. if (en != null && en.MoveNext())
  176. {
  177. Match m = (Match)en.Current;
  178. matchPos = m.Index - 1;
  179. }
  180. if (first) {
  181. if ( ( !xmlCharType.IsStartNCNameCharXml4e( name[0] ) && (local || (!local && name[0] != ':'))) ||
  182. matchPos == 0) {
  183. if (bufBld == null) {
  184. bufBld = new StringBuilder(length + 20);
  185. }
  186. bufBld.Append("_x");
  187. if (length > 1 && XmlCharType.IsHighSurrogate(name[0]) && XmlCharType.IsLowSurrogate(name[1]) ) {
  188. int x = name[0];
  189. int y = name[1];
  190. Int32 u = XmlCharType.CombineSurrogateChar(y, x);
  191. bufBld.Append(u.ToString("X8", CultureInfo.InvariantCulture));
  192. position ++;
  193. copyPosition = 2;
  194. }
  195. else {
  196. bufBld.Append(((Int32)name[0]).ToString("X4", CultureInfo.InvariantCulture));
  197. copyPosition = 1;
  198. }
  199. bufBld.Append("_");
  200. position ++;
  201. if (matchPos == 0)
  202. if (en.MoveNext())
  203. {
  204. Match m = (Match)en.Current;
  205. matchPos = m.Index - 1;
  206. }
  207. }
  208. }
  209. for (; position < length; position ++) {
  210. if ((local && !xmlCharType.IsNCNameCharXml4e(name[position])) ||
  211. (!local && !xmlCharType.IsNameCharXml4e(name[position])) ||
  212. (matchPos == position))
  213. {
  214. if (bufBld == null) {
  215. bufBld = new StringBuilder(length + 20);
  216. }
  217. if (matchPos == position)
  218. if (en.MoveNext())
  219. {
  220. Match m = (Match)en.Current;
  221. matchPos = m.Index - 1;
  222. }
  223. bufBld.Append(name, copyPosition, position - copyPosition);
  224. bufBld.Append("_x");
  225. if ((length > position + 1) && XmlCharType.IsHighSurrogate(name[position]) && XmlCharType.IsLowSurrogate(name[position + 1])) {
  226. int x = name[position];
  227. int y = name[position + 1];
  228. Int32 u = XmlCharType.CombineSurrogateChar(y, x);
  229. bufBld.Append(u.ToString("X8", CultureInfo.InvariantCulture));
  230. copyPosition = position + 2;
  231. position ++;
  232. }
  233. else {
  234. bufBld.Append(((Int32)name[position]).ToString("X4", CultureInfo.InvariantCulture));
  235. copyPosition = position + 1;
  236. }
  237. bufBld.Append("_");
  238. }
  239. }
  240. if (copyPosition == 0) {
  241. return name;
  242. }
  243. else {
  244. if (copyPosition < length) {
  245. bufBld.Append(name, copyPosition, length - copyPosition);
  246. }
  247. return bufBld.ToString();
  248. }
  249. }
  250. private static readonly int c_EncodedCharLength = 7; // ("_xFFFF_".Length);
  251. private static volatile Regex c_EncodeCharPattern;
  252. private static volatile Regex c_DecodeCharPattern;
  253. private static int FromHex(char digit) {
  254. return(digit <= '9')
  255. ? ((int)digit - (int)'0')
  256. : (((digit <= 'F')
  257. ? ((int)digit - (int)'A')
  258. : ((int)digit - (int)'a'))
  259. + 10);
  260. }
  261. internal static byte[] FromBinHexString(string s) {
  262. return FromBinHexString(s, true);
  263. }
  264. internal static byte[] FromBinHexString(string s, bool allowOddCount ) {
  265. if (s == null) {
  266. throw new ArgumentNullException("s");
  267. }
  268. return BinHexDecoder.Decode(s.ToCharArray(), allowOddCount);
  269. }
  270. internal static string ToBinHexString(byte[] inArray) {
  271. if (inArray == null) {
  272. throw new ArgumentNullException("inArray");
  273. }
  274. return BinHexEncoder.Encode(inArray, 0, inArray.Length);
  275. }
  276. //
  277. // Verification methods for strings
  278. //
  279. /// <include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.VerifyName"]/*' />
  280. /// <devdoc>
  281. /// <para>
  282. /// </para>
  283. /// </devdoc>
  284. public static string VerifyName(string name) {
  285. if (name == null) {
  286. throw new ArgumentNullException("name");
  287. }
  288. if (name.Length == 0) {
  289. throw new ArgumentNullException("name", Res.GetString(Res.Xml_EmptyName));
  290. }
  291. // parse name
  292. int endPos = ValidateNames.ParseNameNoNamespaces(name, 0);
  293. if (endPos != name.Length) {
  294. // did not parse to the end -> there is invalid character at endPos
  295. throw CreateInvalidNameCharException(name, endPos, ExceptionType.XmlException);
  296. }
  297. return name;
  298. }
  299. #if !SILVERLIGHT
  300. internal static Exception TryVerifyName(string name) {
  301. if (name == null || name.Length == 0) {
  302. return new XmlException(Res.Xml_EmptyName, string.Empty);
  303. }
  304. int endPos = ValidateNames.ParseNameNoNamespaces(name, 0);
  305. if (endPos != name.Length) {
  306. return new XmlException(endPos == 0 ? Res.Xml_BadStartNameChar : Res.Xml_BadNameChar, XmlException.BuildCharExceptionArgs(name, endPos));
  307. }
  308. return null;
  309. }
  310. internal static string VerifyQName(string name) {
  311. return VerifyQName(name, ExceptionType.XmlException);
  312. }
  313. #endif
  314. #if SILVERLIGHT
  315. internal static string VerifyQName(string name, ExceptionType exceptionType) {
  316. #else
  317. internal static unsafe string VerifyQName(string name, ExceptionType exceptionType) {
  318. #endif
  319. if (name == null || name.Length == 0) {
  320. throw new ArgumentNullException("name");
  321. }
  322. int colonPosition = -1;
  323. int endPos = ValidateNames.ParseQName(name, 0, out colonPosition);
  324. if (endPos != name.Length) {
  325. throw CreateException(Res.Xml_BadNameChar, XmlException.BuildCharExceptionArgs(name, endPos), exceptionType, 0, endPos + 1);
  326. }
  327. return name;
  328. }
  329. /// <include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.VerifyNCName"]/*' />
  330. /// <devdoc>
  331. /// <para>
  332. /// </para>
  333. /// </devdoc>
  334. public static string VerifyNCName(string name) {
  335. return VerifyNCName(name, ExceptionType.XmlException);
  336. }
  337. internal static string VerifyNCName(string name, ExceptionType exceptionType) {
  338. if (name == null) {
  339. throw new ArgumentNullException("name");
  340. }
  341. if (name.Length == 0) {
  342. throw new ArgumentNullException("name", Res.GetString(Res.Xml_EmptyLocalName));
  343. }
  344. int end = ValidateNames.ParseNCName(name, 0);
  345. if (end != name.Length) {
  346. // If the string is not a valid NCName, then throw or return false
  347. throw CreateInvalidNameCharException(name, end, exceptionType);
  348. }
  349. return name;
  350. }
  351. #if !SILVERLIGHT
  352. internal static Exception TryVerifyNCName(string name) {
  353. int len = ValidateNames.ParseNCName(name);
  354. if (len == 0 || len != name.Length) {
  355. return ValidateNames.GetInvalidNameException(name, 0, len);
  356. }
  357. return null;
  358. }
  359. /// <include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.VerifyTOKEN"]/*' />
  360. /// <devdoc>
  361. /// <para>
  362. /// </para>
  363. /// </devdoc>
  364. public static string VerifyTOKEN(string token) {
  365. if (token == null || token.Length == 0) {
  366. return token;
  367. }
  368. if (token[0] == ' ' || token[token.Length - 1] == ' ' || token.IndexOfAny(crt) != -1 || token.IndexOf(" ", StringComparison.Ordinal) != -1) {
  369. throw new XmlException(Res.Sch_NotTokenString, token);
  370. }
  371. return token;
  372. }
  373. internal static Exception TryVerifyTOKEN(string token) {
  374. if (token == null || token.Length == 0) {
  375. return null;
  376. }
  377. if (token[0] == ' ' || token[token.Length - 1] == ' ' || token.IndexOfAny(crt) != -1 || token.IndexOf(" ", StringComparison.Ordinal) != -1) {
  378. return new XmlException(Res.Sch_NotTokenString, token);
  379. }
  380. return null;
  381. }
  382. #endif
  383. /// <include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.VerifyNMTOKEN"]/*' />
  384. /// <devdoc>
  385. /// <para>
  386. /// </para>
  387. /// </devdoc>
  388. public static string VerifyNMTOKEN(string name) {
  389. return VerifyNMTOKEN(name, ExceptionType.XmlException);
  390. }
  391. internal static string VerifyNMTOKEN(string name, ExceptionType exceptionType) {
  392. if (name == null) {
  393. throw new ArgumentNullException("name");
  394. }
  395. if (name.Length == 0) {
  396. throw CreateException(Res.Xml_InvalidNmToken, name, exceptionType);
  397. }
  398. int endPos = ValidateNames.ParseNmtokenNoNamespaces(name, 0);
  399. if (endPos != name.Length) {
  400. throw CreateException(Res.Xml_BadNameChar, XmlException.BuildCharExceptionArgs(name, endPos), exceptionType, 0, endPos + 1);
  401. }
  402. return name;
  403. }
  404. #if !SILVERLIGHT
  405. internal static Exception TryVerifyNMTOKEN(string name) {
  406. if (name == null || name.Length == 0) {
  407. return new XmlException(Res.Xml_EmptyName, string.Empty);
  408. }
  409. int endPos = ValidateNames.ParseNmtokenNoNamespaces(name, 0);
  410. if (endPos != name.Length) {
  411. return new XmlException(Res.Xml_BadNameChar, XmlException.BuildCharExceptionArgs(name, endPos));
  412. }
  413. return null;
  414. }
  415. internal static string VerifyNormalizedString(string str) {
  416. if (str.IndexOfAny(crt) != -1) {
  417. throw new XmlSchemaException(Res.Sch_NotNormalizedString, str);
  418. }
  419. return str;
  420. }
  421. internal static Exception TryVerifyNormalizedString(string str) {
  422. if (str.IndexOfAny(crt) != -1) {
  423. return new XmlSchemaException(Res.Sch_NotNormalizedString, str);
  424. }
  425. return null;
  426. }
  427. #endif
  428. // Verification method for XML characters as defined in XML spec production [2] Char.
  429. // Throws XmlException if invalid character is found, otherwise returns the input string.
  430. public static string VerifyXmlChars(string content) {
  431. if (content == null) {
  432. throw new ArgumentNullException("content");
  433. }
  434. VerifyCharData(content, ExceptionType.XmlException);
  435. return content;
  436. }
  437. // Verification method for XML public ID characters as defined in XML spec production [13] PubidChar.
  438. // Throws XmlException if invalid character is found, otherwise returns the input string.
  439. public static string VerifyPublicId(string publicId) {
  440. if (publicId == null) {
  441. throw new ArgumentNullException("publicId");
  442. }
  443. // returns the position of invalid character or -1
  444. int pos = xmlCharType.IsPublicId(publicId);
  445. if (pos != -1) {
  446. throw CreateInvalidCharException(publicId, pos, ExceptionType.XmlException);
  447. }
  448. return publicId;
  449. }
  450. // Verification method for XML whitespace characters as defined in XML spec production [3] S.
  451. // Throws XmlException if invalid character is found, otherwise returns the input string.
  452. public static string VerifyWhitespace(string content) {
  453. if (content == null) {
  454. throw new ArgumentNullException("content");
  455. }
  456. // returns the position of invalid character or -1
  457. int pos = xmlCharType.IsOnlyWhitespaceWithPos(content);
  458. if (pos != -1) {
  459. throw new XmlException(Res.Xml_InvalidWhitespaceCharacter, XmlException.BuildCharExceptionArgs(content, pos), 0, pos + 1);
  460. }
  461. return content;
  462. }
  463. //
  464. // Verification methods for single characters and surrogates
  465. //
  466. // In cases where the direct call into XmlCharType would not get automatically inlined (because of the use of byte* field),
  467. // direct access to the XmlCharType.charProperties is used instead (= manual inlining).
  468. //
  469. // Start name character types - as defined in Namespaces XML 1.0 spec (second edition) production [6] NCNameStartChar
  470. // combined with the production [4] NameStartChar of XML 1.0 spec
  471. public static unsafe bool IsStartNCNameChar(char ch) {
  472. #if SILVERLIGHT
  473. return xmlCharType.IsStartNCNameSingleChar(ch);
  474. #else
  475. return (xmlCharType.charProperties[ch] & XmlCharType.fNCStartNameSC) != 0;
  476. #endif
  477. }
  478. #if XML10_FIFTH_EDITION
  479. public static bool IsStartNCNameSurrogatePair(char lowChar, char highChar) {
  480. return xmlCharType.IsNCNameSurrogateChar(lowChar, highChar);
  481. }
  482. #endif
  483. // Name character types - as defined in Namespaces XML 1.0 spec (second edition) production [6] NCNameStartChar
  484. // combined with the production [4] NameChar of XML 1.0 spec
  485. public static unsafe bool IsNCNameChar(char ch) {
  486. #if SILVERLIGHT
  487. return xmlCharType.IsNCNameSingleChar(ch);
  488. #else
  489. return (xmlCharType.charProperties[ch] & XmlCharType.fNCNameSC) != 0;
  490. #endif
  491. }
  492. #if XML10_FIFTH_EDITION
  493. public static bool IsNCNameSurrogatePair(char lowChar, char highChar) {
  494. return xmlCharType.IsNCNameSurrogateChar(lowChar, highChar);
  495. }
  496. #endif
  497. // Valid XML character – as defined in XML 1.0 spec (fifth edition) production [2] Char
  498. public static unsafe bool IsXmlChar(char ch) {
  499. #if SILVERLIGHT
  500. return xmlCharType.IsCharData(ch);
  501. #else
  502. return (xmlCharType.charProperties[ch] & XmlCharType.fCharData) != 0;
  503. #endif
  504. }
  505. public static bool IsXmlSurrogatePair(char lowChar, char highChar) {
  506. return XmlCharType.IsHighSurrogate(highChar) && XmlCharType.IsLowSurrogate(lowChar);
  507. }
  508. // Valid PUBLIC ID character – as defined in XML 1.0 spec (fifth edition) production [13] PublidChar
  509. public static bool IsPublicIdChar(char ch) {
  510. return xmlCharType.IsPubidChar(ch);
  511. }
  512. // Valid Xml white space – as defined in XML 1.0 spec (fifth edition) production [3] S
  513. public static unsafe bool IsWhitespaceChar(char ch) {
  514. #if SILVERLIGHT
  515. return xmlCharType.IsWhiteSpace(ch);
  516. #else
  517. return (xmlCharType.charProperties[ch] & XmlCharType.fWhitespace) != 0;
  518. #endif
  519. }
  520. // Value convertors:
  521. //
  522. // String representation of Base types in XML (xsd) sometimes differ from
  523. // one common language runtime offer and for all types it has to be locale independent.
  524. // o -- means that XmlConvert pass through to common language runtime converter with InvariantInfo FormatInfo
  525. // x -- means we doing something special to make a convertion.
  526. //
  527. // From: To: Bol Chr SBy Byt I16 U16 I32 U32 I64 U64 Sgl Dbl Dec Dat Tim Str uid
  528. // ------------------------------------------------------------------------------
  529. // Boolean x
  530. // Char o
  531. // SByte o
  532. // Byte o
  533. // Int16 o
  534. // UInt16 o
  535. // Int32 o
  536. // UInt32 o
  537. // Int64 o
  538. // UInt64 o
  539. // Single x
  540. // Double x
  541. // Decimal o
  542. // DateTime x
  543. // String x o o o o o o o o o o x x o o x
  544. // Guid x
  545. // -----------------------------------------------------------------------------
  546. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString"]/*' />
  547. /// <devdoc>
  548. /// <para>[To be supplied.]</para>
  549. /// </devdoc>
  550. public static string ToString(Boolean value) {
  551. return value ? "true" : "false";
  552. }
  553. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString1"]/*' />
  554. /// <devdoc>
  555. /// <para>[To be supplied.]</para>
  556. /// </devdoc>
  557. public static string ToString(Char value) {
  558. return value.ToString(null);
  559. }
  560. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString2"]/*' />
  561. /// <devdoc>
  562. /// <para>[To be supplied.]</para>
  563. /// </devdoc>
  564. public static string ToString(Decimal value) {
  565. return value.ToString(null, NumberFormatInfo.InvariantInfo);
  566. }
  567. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString3"]/*' />
  568. /// <devdoc>
  569. /// <para>[To be supplied.]</para>
  570. /// </devdoc>
  571. [CLSCompliant(false)]
  572. public static string ToString(SByte value) {
  573. return value.ToString(null, NumberFormatInfo.InvariantInfo);
  574. }
  575. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString4"]/*' />
  576. /// <devdoc>
  577. /// <para>[To be supplied.]</para>
  578. /// </devdoc>
  579. public static string ToString(Int16 value) {
  580. return value.ToString(null, NumberFormatInfo.InvariantInfo);
  581. }
  582. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString5"]/*' />
  583. /// <devdoc>
  584. /// <para>[To be supplied.]</para>
  585. /// </devdoc>
  586. public static string ToString(Int32 value) {
  587. return value.ToString(null, NumberFormatInfo.InvariantInfo);
  588. }
  589. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString15"]/*' />
  590. /// <devdoc>
  591. /// <para>[To be supplied.]</para>
  592. /// </devdoc>
  593. public static string ToString(Int64 value) {
  594. return value.ToString(null, NumberFormatInfo.InvariantInfo);
  595. }
  596. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString6"]/*' />
  597. /// <devdoc>
  598. /// <para>[To be supplied.]</para>
  599. /// </devdoc>
  600. public static string ToString(Byte value) {
  601. return value.ToString(null, NumberFormatInfo.InvariantInfo);
  602. }
  603. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString7"]/*' />
  604. /// <devdoc>
  605. /// <para>[To be supplied.]</para>
  606. /// </devdoc>
  607. [CLSCompliant(false)]
  608. public static string ToString(UInt16 value) {
  609. return value.ToString(null, NumberFormatInfo.InvariantInfo);
  610. }
  611. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString8"]/*' />
  612. /// <devdoc>
  613. /// <para>[To be supplied.]</para>
  614. /// </devdoc>
  615. [CLSCompliant(false)]
  616. public static string ToString(UInt32 value) {
  617. return value.ToString(null, NumberFormatInfo.InvariantInfo);
  618. }
  619. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString16"]/*' />
  620. /// <devdoc>
  621. /// <para>[To be supplied.]</para>
  622. /// </devdoc>
  623. [CLSCompliant(false)]
  624. public static string ToString(UInt64 value) {
  625. return value.ToString(null, NumberFormatInfo.InvariantInfo);
  626. }
  627. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString9"]/*' />
  628. /// <devdoc>
  629. /// <para>[To be supplied.]</para>
  630. /// </devdoc>
  631. public static string ToString(Single value) {
  632. if (Single.IsNegativeInfinity(value)) return "-INF";
  633. if (Single.IsPositiveInfinity(value)) return "INF";
  634. if (IsNegativeZero((double)value)) {
  635. return("-0");
  636. }
  637. return value.ToString("R", NumberFormatInfo.InvariantInfo);
  638. }
  639. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString10"]/*' />
  640. /// <devdoc>
  641. /// <para>[To be supplied.]</para>
  642. /// </devdoc>
  643. public static string ToString(Double value) {
  644. if (Double.IsNegativeInfinity(value)) return "-INF";
  645. if (Double.IsPositiveInfinity(value)) return "INF";
  646. if (IsNegativeZero(value)) {
  647. return("-0");
  648. }
  649. return value.ToString("R", NumberFormatInfo.InvariantInfo);
  650. }
  651. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString11"]/*' />
  652. /// <devdoc>
  653. /// <para>[To be supplied.]</para>
  654. /// </devdoc>
  655. public static string ToString(TimeSpan value) {
  656. return new XsdDuration(value).ToString();
  657. }
  658. #if !SILVERLIGHT
  659. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString12"]/*' />
  660. /// <devdoc>
  661. /// <para>[To be supplied.]</para>
  662. /// </devdoc>
  663. [Obsolete("Use XmlConvert.ToString() that takes in XmlDateTimeSerializationMode")]
  664. public static string ToString(DateTime value) {
  665. return ToString(value, "yyyy-MM-ddTHH:mm:ss.fffffffzzzzzz");
  666. }
  667. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString13"]/*' />
  668. /// <devdoc>
  669. /// <para>[To be supplied.]</para>
  670. /// </devdoc>
  671. public static string ToString(DateTime value, string format) {
  672. return value.ToString(format, DateTimeFormatInfo.InvariantInfo);
  673. }
  674. #endif
  675. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString14"]/*' />
  676. /// <devdoc>
  677. /// <para>[To be supplied.]</para>
  678. /// </devdoc>
  679. public static string ToString(DateTime value, XmlDateTimeSerializationMode dateTimeOption) {
  680. switch(dateTimeOption) {
  681. case XmlDateTimeSerializationMode.Local:
  682. value = SwitchToLocalTime(value);
  683. break;
  684. case XmlDateTimeSerializationMode.Utc:
  685. value = SwitchToUtcTime(value);
  686. break;
  687. case XmlDateTimeSerializationMode.Unspecified:
  688. value = new DateTime(value.Ticks, DateTimeKind.Unspecified);
  689. break;
  690. case XmlDateTimeSerializationMode.RoundtripKind:
  691. break;
  692. default:
  693. throw new ArgumentException(Res.GetString(Res.Sch_InvalidDateTimeOption, dateTimeOption, "dateTimeOption"));
  694. }
  695. XsdDateTime xsdDateTime = new XsdDateTime(value, XsdDateTimeFlags.DateTime);
  696. return xsdDateTime.ToString();
  697. }
  698. public static string ToString(DateTimeOffset value) {
  699. XsdDateTime xsdDateTime = new XsdDateTime(value);
  700. return xsdDateTime.ToString();
  701. }
  702. public static string ToString(DateTimeOffset value, string format) {
  703. return value.ToString( format, DateTimeFormatInfo.InvariantInfo );
  704. }
  705. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToString15"]/*' />
  706. /// <devdoc>
  707. /// <para>[To be supplied.]</para>
  708. /// </devdoc>
  709. public static string ToString(Guid value) {
  710. return value.ToString();
  711. }
  712. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToBoolean"]/*' />
  713. /// <devdoc>
  714. /// <para>[To be supplied.]</para>
  715. /// </devdoc>
  716. public static Boolean ToBoolean (string s) {
  717. s = TrimString(s);
  718. if (s == "1" || s == "true" ) return true;
  719. if (s == "0" || s == "false") return false;
  720. throw new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Boolean"));
  721. }
  722. #if !SILVERLIGHT
  723. internal static Exception TryToBoolean(string s, out Boolean result) {
  724. s = TrimString(s);
  725. if (s == "0" || s == "false") {
  726. result = false;
  727. return null;
  728. }
  729. else if (s == "1" || s == "true") {
  730. result = true;
  731. return null;
  732. }
  733. result = false;
  734. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Boolean"));
  735. }
  736. #endif
  737. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToChar"]/*' />
  738. /// <devdoc>
  739. /// <para>[To be supplied.]</para>
  740. /// </devdoc>
  741. public static Char ToChar (string s) {
  742. if (s == null) {
  743. throw new ArgumentNullException("s");
  744. }
  745. if (s.Length != 1) {
  746. throw new FormatException(Res.GetString(Res.XmlConvert_NotOneCharString));
  747. }
  748. return s[0];
  749. }
  750. #if !SILVERLIGHT
  751. internal static Exception TryToChar(string s, out Char result) {
  752. if (!Char.TryParse(s, out result)) {
  753. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Char"));
  754. }
  755. return null;
  756. }
  757. #endif
  758. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToDecimal"]/*' />
  759. /// <devdoc>
  760. /// <para>[To be supplied.]</para>
  761. /// </devdoc>
  762. public static Decimal ToDecimal (string s) {
  763. return Decimal.Parse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowDecimalPoint|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
  764. }
  765. #if !SILVERLIGHT
  766. internal static Exception TryToDecimal(string s, out Decimal result) {
  767. if (!Decimal.TryParse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowDecimalPoint|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result)) {
  768. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Decimal"));
  769. }
  770. return null;
  771. }
  772. internal static Decimal ToInteger (string s) {
  773. return Decimal.Parse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
  774. }
  775. internal static Exception TryToInteger(string s, out Decimal result) {
  776. if (!Decimal.TryParse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result)) {
  777. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Integer"));
  778. }
  779. return null;
  780. }
  781. #endif
  782. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToSByte"]/*' />
  783. /// <devdoc>
  784. /// <para>[To be supplied.]</para>
  785. /// </devdoc>
  786. [CLSCompliant(false)]
  787. public static SByte ToSByte (string s) {
  788. return SByte.Parse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
  789. }
  790. #if !SILVERLIGHT
  791. internal static Exception TryToSByte(string s, out SByte result) {
  792. if (!SByte.TryParse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result)) {
  793. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "SByte"));
  794. }
  795. return null;
  796. }
  797. #endif
  798. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToInt16"]/*' />
  799. /// <devdoc>
  800. /// <para>[To be supplied.]</para>
  801. /// </devdoc>
  802. public static Int16 ToInt16 (string s) {
  803. return Int16.Parse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
  804. }
  805. #if !SILVERLIGHT
  806. internal static Exception TryToInt16(string s, out Int16 result) {
  807. if (!Int16.TryParse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result)) {
  808. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Int16"));
  809. }
  810. return null;
  811. }
  812. #endif
  813. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToInt32"]/*' />
  814. /// <devdoc>
  815. /// <para>[To be supplied.]</para>
  816. /// </devdoc>
  817. public static Int32 ToInt32 (string s) {
  818. return Int32.Parse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
  819. }
  820. #if !SILVERLIGHT
  821. internal static Exception TryToInt32(string s, out Int32 result) {
  822. if (!Int32.TryParse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result)) {
  823. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Int32"));
  824. }
  825. return null;
  826. }
  827. #endif
  828. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToInt64"]/*' />
  829. /// <devdoc>
  830. /// <para>[To be supplied.]</para>
  831. /// </devdoc>
  832. public static Int64 ToInt64 (string s) {
  833. return Int64.Parse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
  834. }
  835. #if !SILVERLIGHT
  836. internal static Exception TryToInt64(string s, out Int64 result) {
  837. if (!Int64.TryParse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result)) {
  838. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Int64"));
  839. }
  840. return null;
  841. }
  842. #endif
  843. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToByte"]/*' />
  844. /// <devdoc>
  845. /// <para>[To be supplied.]</para>
  846. /// </devdoc>
  847. public static Byte ToByte (string s) {
  848. return Byte.Parse(s, NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
  849. }
  850. #if !SILVERLIGHT
  851. internal static Exception TryToByte(string s, out Byte result) {
  852. if (!Byte.TryParse(s, NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result)) {
  853. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Byte"));
  854. }
  855. return null;
  856. }
  857. #endif
  858. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToUInt16"]/*' />
  859. /// <devdoc>
  860. /// <para>[To be supplied.]</para>
  861. /// </devdoc>
  862. [CLSCompliant(false)]
  863. public static UInt16 ToUInt16 (string s) {
  864. return UInt16.Parse(s, NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
  865. }
  866. #if !SILVERLIGHT
  867. internal static Exception TryToUInt16(string s, out UInt16 result) {
  868. if (!UInt16.TryParse(s, NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result)) {
  869. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "UInt16"));
  870. }
  871. return null;
  872. }
  873. #endif
  874. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToUInt32"]/*' />
  875. /// <devdoc>
  876. /// <para>[To be supplied.]</para>
  877. /// </devdoc>
  878. [CLSCompliant(false)]
  879. public static UInt32 ToUInt32 (string s) {
  880. return UInt32.Parse(s, NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
  881. }
  882. #if !SILVERLIGHT
  883. internal static Exception TryToUInt32(string s, out UInt32 result) {
  884. if (!UInt32.TryParse(s, NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result)) {
  885. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "UInt32"));
  886. }
  887. return null;
  888. }
  889. #endif
  890. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToUInt64"]/*' />
  891. /// <devdoc>
  892. /// <para>[To be supplied.]</para>
  893. /// </devdoc>
  894. [CLSCompliant(false)]
  895. public static UInt64 ToUInt64 (string s) {
  896. return UInt64.Parse(s, NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
  897. }
  898. #if !SILVERLIGHT
  899. internal static Exception TryToUInt64(string s, out UInt64 result) {
  900. if (!UInt64.TryParse(s, NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result)) {
  901. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "UInt64"));
  902. }
  903. return null;
  904. }
  905. #endif
  906. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToSingle"]/*' />
  907. /// <devdoc>
  908. /// <para>[To be supplied.]</para>
  909. /// </devdoc>
  910. public static Single ToSingle (string s) {
  911. s = TrimString(s);
  912. if(s == "-INF") return Single.NegativeInfinity;
  913. if(s == "INF") return Single.PositiveInfinity;
  914. float f = Single.Parse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowDecimalPoint|NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo);
  915. if (f == 0 && s[0] == '-') {
  916. return -0f;
  917. }
  918. return f;
  919. }
  920. #if !SILVERLIGHT
  921. internal static Exception TryToSingle(string s, out Single result) {
  922. s = TrimString(s);
  923. if (s == "-INF") {
  924. result = Single.NegativeInfinity;
  925. return null;
  926. }
  927. else if (s == "INF") {
  928. result = Single.PositiveInfinity;
  929. return null;
  930. }
  931. else if (!Single.TryParse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowDecimalPoint|NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo, out result)) {
  932. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Single"));
  933. }
  934. if (result == 0 && s[0] == '-') {
  935. result = -0f;
  936. }
  937. return null;
  938. }
  939. #endif
  940. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToDouble"]/*' />
  941. /// <devdoc>
  942. /// <para>[To be supplied.]</para>
  943. /// </devdoc>
  944. public static Double ToDouble(string s) {
  945. s = TrimString(s);
  946. if(s == "-INF") return Double.NegativeInfinity;
  947. if(s == "INF") return Double.PositiveInfinity;
  948. double dVal = Double.Parse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowDecimalPoint|NumberStyles.AllowExponent|NumberStyles.AllowLeadingWhite|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
  949. if (dVal == 0 && s[0] == '-') {
  950. return -0d;
  951. }
  952. return dVal;
  953. }
  954. #if !SILVERLIGHT
  955. internal static Exception TryToDouble (string s, out double result) {
  956. s = TrimString(s);
  957. if (s == "-INF") {
  958. result = Double.NegativeInfinity;
  959. return null;
  960. }
  961. else if (s == "INF") {
  962. result = Double.PositiveInfinity;
  963. return null;
  964. }
  965. else if (!Double.TryParse(s, NumberStyles.AllowLeadingSign|NumberStyles.AllowDecimalPoint|NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo, out result)) {
  966. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Double"));
  967. }
  968. if (result == 0 && s[0] == '-') {
  969. result = -0d;
  970. }
  971. return null;
  972. }
  973. #endif
  974. #if !SILVERLIGHT
  975. internal static Double ToXPathDouble (Object o) {
  976. string str = o as string;
  977. if (str != null) {
  978. str = TrimString(str);
  979. if (str.Length != 0 && str[0] != '+') {
  980. double d;
  981. if (Double.TryParse(str, NumberStyles.AllowLeadingSign|NumberStyles.AllowDecimalPoint|NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out d)) {
  982. return d;
  983. }
  984. }
  985. return Double.NaN;
  986. }
  987. if (o is double) {
  988. return (double) o;
  989. }
  990. if (o is bool) {
  991. return ((bool) o) ? 1.0 : 0.0;
  992. }
  993. try {
  994. return Convert.ToDouble(o, NumberFormatInfo.InvariantInfo);
  995. } catch ( FormatException ) {
  996. } catch ( OverflowException ) {
  997. } catch ( ArgumentNullException ) {}
  998. return Double.NaN;
  999. }
  1000. internal static String ToXPathString(Object value){
  1001. string s = value as string;
  1002. if ( s != null ) {
  1003. return s;
  1004. }
  1005. else if ( value is double ) {
  1006. return ((double)value).ToString("R", NumberFormatInfo.InvariantInfo);
  1007. }
  1008. else if ( value is bool ) {
  1009. return (bool)value ? "true" : "false";
  1010. }
  1011. else {
  1012. return Convert.ToString(value, NumberFormatInfo.InvariantInfo);
  1013. }
  1014. }
  1015. internal static Double XPathRound(Double value) {
  1016. double temp = Math.Round(value);
  1017. return (value - temp == 0.5) ? temp + 1 : temp;
  1018. }
  1019. #endif
  1020. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToTimeSpan"]/*' />
  1021. /// <devdoc>
  1022. /// <para>[To be supplied.]</para>
  1023. /// </devdoc>
  1024. public static TimeSpan ToTimeSpan(string s) {
  1025. XsdDuration duration;
  1026. TimeSpan timeSpan;
  1027. try {
  1028. duration = new XsdDuration(s);
  1029. }
  1030. catch (Exception) {
  1031. // Remap exception for v1 compatibility
  1032. throw new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "TimeSpan"));
  1033. }
  1034. timeSpan = duration.ToTimeSpan();
  1035. return timeSpan;
  1036. }
  1037. #if !SILVERLIGHT
  1038. internal static Exception TryToTimeSpan(string s, out TimeSpan result) {
  1039. XsdDuration duration;
  1040. Exception exception;
  1041. exception = XsdDuration.TryParse(s, out duration);
  1042. if (exception != null) {
  1043. result = TimeSpan.MinValue;
  1044. return exception;
  1045. }
  1046. else {
  1047. return duration.TryToTimeSpan(out result);
  1048. }
  1049. }
  1050. #endif
  1051. #if !SILVERLIGHT
  1052. // use AllDateTimeFormats property to access the formats
  1053. static volatile string[] s_allDateTimeFormats;
  1054. // NOTE: Do not use this property for reference comparison. It may not be unique.
  1055. static string[] AllDateTimeFormats {
  1056. get {
  1057. if ( s_allDateTimeFormats == null ) {
  1058. CreateAllDateTimeFormats();
  1059. }
  1060. return s_allDateTimeFormats;
  1061. }
  1062. }
  1063. static void CreateAllDateTimeFormats() {
  1064. if ( s_allDateTimeFormats == null ) {
  1065. // no locking; the array is immutable so it's not a problem that it may get initialized more than once
  1066. s_allDateTimeFormats = new string[] {
  1067. "yyyy-MM-ddTHH:mm:ss.FFFFFFFzzzzzz", //dateTime
  1068. "yyyy-MM-ddTHH:mm:ss.FFFFFFF",
  1069. "yyyy-MM-ddTHH:mm:ss.FFFFFFFZ",
  1070. "HH:mm:ss.FFFFFFF", //time
  1071. "HH:mm:ss.FFFFFFFZ",
  1072. "HH:mm:ss.FFFFFFFzzzzzz",
  1073. "yyyy-MM-dd", // date
  1074. "yyyy-MM-ddZ",
  1075. "yyyy-MM-ddzzzzzz",
  1076. "yyyy-MM", // yearMonth
  1077. "yyyy-MMZ",
  1078. "yyyy-MMzzzzzz",
  1079. "yyyy", // year
  1080. "yyyyZ",
  1081. "yyyyzzzzzz",
  1082. "--MM-dd", // monthDay
  1083. "--MM-ddZ",
  1084. "--MM-ddzzzzzz",
  1085. "---dd", // day
  1086. "---ddZ",
  1087. "---ddzzzzzz",
  1088. "--MM--", // month
  1089. "--MM--Z",
  1090. "--MM--zzzzzz",
  1091. };
  1092. }
  1093. }
  1094. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToDateTime"]/*' />
  1095. /// <devdoc>
  1096. /// <para>[To be supplied.]</para>
  1097. /// </devdoc>
  1098. [Obsolete("Use XmlConvert.ToDateTime() that takes in XmlDateTimeSerializationMode")]
  1099. public static DateTime ToDateTime(string s) {
  1100. return ToDateTime(s, AllDateTimeFormats);
  1101. }
  1102. #endif
  1103. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToDateTime1"]/*' />
  1104. /// <devdoc>
  1105. /// <para>[To be supplied.]</para>
  1106. /// </devdoc>
  1107. public static DateTime ToDateTime(string s, string format) {
  1108. return DateTime.ParseExact(s, format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowLeadingWhite|DateTimeStyles.AllowTrailingWhite);
  1109. }
  1110. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToDateTime2"]/*' />
  1111. /// <devdoc>
  1112. /// <para>[To be supplied.]</para>
  1113. /// </devdoc>
  1114. public static DateTime ToDateTime(string s, string[] formats) {
  1115. return DateTime.ParseExact(s, formats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowLeadingWhite|DateTimeStyles.AllowTrailingWhite);
  1116. }
  1117. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToDateTime3"]/*' />
  1118. /// <devdoc>
  1119. /// <para>[To be supplied.]</para>
  1120. /// </devdoc>
  1121. public static DateTime ToDateTime(string s, XmlDateTimeSerializationMode dateTimeOption) {
  1122. XsdDateTime xsdDateTime = new XsdDateTime(s, XsdDateTimeFlags.AllXsd);
  1123. DateTime dt = (DateTime)xsdDateTime;
  1124. switch (dateTimeOption) {
  1125. case XmlDateTimeSerializationMode.Local:
  1126. dt = SwitchToLocalTime(dt);
  1127. break;
  1128. case XmlDateTimeSerializationMode.Utc:
  1129. dt = SwitchToUtcTime(dt);
  1130. break;
  1131. case XmlDateTimeSerializationMode.Unspecified:
  1132. dt = new DateTime(dt.Ticks, DateTimeKind.Unspecified);
  1133. break;
  1134. case XmlDateTimeSerializationMode.RoundtripKind:
  1135. break;
  1136. default:
  1137. throw new ArgumentException(Res.GetString(Res.Sch_InvalidDateTimeOption, dateTimeOption, "dateTimeOption"));
  1138. }
  1139. return dt;
  1140. }
  1141. public static DateTimeOffset ToDateTimeOffset(string s) {
  1142. if (s == null) {
  1143. throw new ArgumentNullException("s");
  1144. }
  1145. XsdDateTime xsdDateTime = new XsdDateTime(s, XsdDateTimeFlags.AllXsd);
  1146. DateTimeOffset dateTimeOffset = (DateTimeOffset)xsdDateTime;
  1147. return dateTimeOffset;
  1148. }
  1149. public static DateTimeOffset ToDateTimeOffset(string s, string format) {
  1150. if ( s == null ) {
  1151. throw new ArgumentNullException( "s" );
  1152. }
  1153. return DateTimeOffset.ParseExact( s, format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite );
  1154. }
  1155. public static DateTimeOffset ToDateTimeOffset(string s, string[] formats) {
  1156. if ( s == null ) {
  1157. throw new ArgumentNullException( "s" );
  1158. }
  1159. return DateTimeOffset.ParseExact( s, formats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite );
  1160. }
  1161. ///<include file='doc\XmlConvert.uex' path='docs/doc[@for="XmlConvert.ToGuid"]/*' />
  1162. /// <devdoc>
  1163. /// <para>[To be supplied.]</para>
  1164. /// </devdoc>
  1165. public static Guid ToGuid (string s) {
  1166. return new Guid(s);
  1167. }
  1168. #if !SILVERLIGHT
  1169. internal static Exception TryToGuid(string s, out Guid result) {
  1170. Exception exception = null;
  1171. result = Guid.Empty;
  1172. try {
  1173. result = new Guid(s);
  1174. }
  1175. catch (ArgumentException) {
  1176. exception = new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Guid"));
  1177. }
  1178. catch (FormatException) {
  1179. exception = new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Guid"));
  1180. }
  1181. return exception;
  1182. }
  1183. #endif
  1184. private static DateTime SwitchToLocalTime(DateTime value) {
  1185. switch (value.Kind) {
  1186. case DateTimeKind.Local:
  1187. return value;
  1188. case DateTimeKind.Unspecified:
  1189. return new DateTime(value.Ticks, DateTimeKind.Local);
  1190. case DateTimeKind.Utc:
  1191. return value.ToLocalTime();
  1192. }
  1193. return value;
  1194. }
  1195. private static DateTime SwitchToUtcTime(DateTime value) {
  1196. switch (value.Kind) {
  1197. case DateTimeKind.Utc:
  1198. return value;
  1199. case DateTimeKind.Unspecified:
  1200. return new DateTime(value.Ticks, DateTimeKind.Utc);
  1201. case DateTimeKind.Local:
  1202. return value.ToUniversalTime();
  1203. }
  1204. return value;
  1205. }
  1206. internal static Uri ToUri(string s) {
  1207. if (s != null && s.Length > 0) { //string.Empty is a valid uri but not " "
  1208. s = TrimString(s);
  1209. if (s.Length == 0 || s.IndexOf("##", StringComparison.Ordinal) != -1) {
  1210. throw new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Uri"));
  1211. }
  1212. }
  1213. Uri uri;
  1214. if (!Uri.TryCreate(s, UriKind.RelativeOrAbsolute, out uri)) {
  1215. throw new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Uri"));
  1216. }
  1217. return uri;
  1218. }
  1219. #if !SILVERLIGHT
  1220. internal static Exception TryToUri(string s, out Uri result) {
  1221. result = null;
  1222. if (s != null && s.Length > 0) { //string.Empty is a valid uri but not " "
  1223. s = TrimString(s);
  1224. if (s.Length == 0 || s.IndexOf("##", StringComparison.Ordinal) != -1) {
  1225. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Uri"));
  1226. }
  1227. }
  1228. if (!Uri.TryCreate(s, UriKind.RelativeOrAbsolute, out result)) {
  1229. return new FormatException(Res.GetString(Res.XmlConvert_BadFormat, s, "Uri"));
  1230. }
  1231. return null;
  1232. }
  1233. #endif
  1234. // Compares the given character interval and string and returns true if the characters are identical
  1235. internal static bool StrEqual( char[] chars, int strPos1, int strLen1, string str2 ) {
  1236. if ( strLen1 != str2.Length ) {
  1237. return false;
  1238. }
  1239. int i = 0;
  1240. while ( i < strLen1 && chars[strPos1+i] == str2[i] ) {
  1241. i++;
  1242. }
  1243. return i == strLen1;
  1244. }
  1245. // XML whitespace characters, <spec>http://www.w3.org/TR/REC-xml#NT-S</spec>
  1246. internal static readonly char[] WhitespaceChars = new char[] { ' ', '\t', '\n', '\r' };
  1247. // Trim a string using XML whitespace characters
  1248. internal static string TrimString(string value) {
  1249. return value.Trim(WhitespaceChars);
  1250. }
  1251. // Trim beginning of a string using XML whitespace characters
  1252. internal static string TrimStringStart(string value) {
  1253. return value.TrimStart(WhitespaceChars);
  1254. }
  1255. // Trim end of a string using XML whitespace characters
  1256. internal static string TrimStringEnd(string value) {
  1257. return value.TrimEnd(WhitespaceChars);
  1258. }
  1259. // Split a string into a whitespace-separated list of tokens
  1260. internal static string[] SplitString(string value) {
  1261. return value.Split(WhitespaceChars, StringSplitOptions.RemoveEmptyEntries);
  1262. }
  1263. internal static string[] SplitString(string value, StringSplitOptions splitStringOptions) {
  1264. return value.Split(WhitespaceChars, splitStringOptions);
  1265. }
  1266. internal static bool IsNegativeZero(double value) {
  1267. // Simple equals function will report that -0 is equal to +0, so compare bits instead
  1268. if (value == 0 && DoubleToInt64Bits(value) == DoubleToInt64Bits(-0e0)) {
  1269. return true;
  1270. }
  1271. return false;
  1272. }
  1273. #if SILVERLIGHT && !SILVERLIGHT_DISABLE_SECURITY
  1274. [System.Security.SecuritySafeCritical]
  1275. #endif
  1276. private static unsafe long DoubleToInt64Bits(double value) {
  1277. // NOTE: BitConverter.DoubleToInt64Bits is missing in Silverlight
  1278. return *((long*)&value);
  1279. }
  1280. internal static void VerifyCharData(string data, ExceptionType exceptionType) {
  1281. VerifyCharData(data, exceptionType, exceptionType);
  1282. }
  1283. #if SILVERLIGHT
  1284. internal static void VerifyCharData( string data, ExceptionType invCharExceptionType, ExceptionType invSurrogateExceptionType ) {
  1285. #else
  1286. internal static unsafe void VerifyCharData( string data, ExceptionType invCharExceptionType, ExceptionType invSurrogateExceptionType ) {
  1287. #endif
  1288. if ( data == null || data.Length == 0 ) {
  1289. return;
  1290. }
  1291. int i = 0;
  1292. int len = data.Length;
  1293. for (;;) {
  1294. #if SILVERLIGHT
  1295. while (i < len && xmlCharType.IsCharData(data[i])) {
  1296. #else
  1297. while ( i < len && ( xmlCharType.charProperties[data[i]] & XmlCharType.fCharData ) != 0 ) {
  1298. #endif
  1299. i++;
  1300. }
  1301. if ( i == len ) {
  1302. return;
  1303. }
  1304. char ch = data[i];
  1305. if ( XmlCharType.IsHighSurrogate( ch ) ) {
  1306. if ( i + 1 == len ) {
  1307. throw CreateException( Res.Xml_InvalidSurrogateMissingLowChar, invSurrogateExceptionType, 0, i + 1 );
  1308. }
  1309. ch = data[i+1];
  1310. if ( XmlCharType.IsLowSurrogate( ch ) ) {
  1311. i += 2;
  1312. continue;
  1313. }
  1314. else {
  1315. throw CreateInvalidSurrogatePairException( data[i+1], data[i], invSurrogateExceptionType, 0, i + 1 );
  1316. }
  1317. }
  1318. throw CreateInvalidCharException( data, i, invCharExceptionType );
  1319. }
  1320. }
  1321. #if SILVERLIGHT
  1322. internal static void VerifyCharData(char[] data, int offset, int len, ExceptionType exceptionType) {
  1323. #else
  1324. internal static unsafe void VerifyCharData( char[] data, int offset, int len, ExceptionType exceptionType ) {
  1325. #endif
  1326. if ( data == null || len == 0 ) {
  1327. return;
  1328. }
  1329. int i = offset;
  1330. int endPos = offset + len;
  1331. for (;;) {
  1332. #if SILVERLIGHT
  1333. while ( i < endPos && xmlCharType.IsCharData(data[i])) {
  1334. #else
  1335. while ( i < endPos && ( xmlCharType.charProperties[data[i]] & XmlCharType.fCharData ) != 0 ) {
  1336. #endif
  1337. i++;
  1338. }
  1339. if ( i == endPos ) {
  1340. return;
  1341. }
  1342. char ch = data[i];
  1343. if ( XmlCharType.IsHighSurrogate( ch ) ) {
  1344. if ( i + 1 == endPos ) {
  1345. throw CreateException(Res.Xml_InvalidSurrogateMissingLowChar, exceptionType, 0, offset - i + 1);
  1346. }
  1347. ch = data[i+1];
  1348. if ( XmlCharType.IsLowSurrogate( ch ) ) {
  1349. i += 2;
  1350. continue;
  1351. }
  1352. else {
  1353. throw CreateInvalidSurrogatePairException(data[i + 1], data[i], exceptionType, 0, offset - i + 1);
  1354. }
  1355. }
  1356. throw CreateInvalidCharException( data, len, i, exceptionType );
  1357. }
  1358. }
  1359. #if !SILVERLIGHT
  1360. internal static string EscapeValueForDebuggerDisplay( string value ) {
  1361. StringBuilder sb = null;
  1362. int i = 0;
  1363. int start = 0;
  1364. while ( i < value.Length ) {
  1365. char ch = value[i];
  1366. if ( (int)ch < 0x20 || ch == '"' ) {
  1367. if ( sb == null ) {
  1368. sb = new StringBuilder( value.Length + 4 );
  1369. }
  1370. if ( i - start > 0 ) {
  1371. sb.Append( value, start, i - start );
  1372. }
  1373. start = i + 1;
  1374. switch ( ch ) {
  1375. case '"':
  1376. sb.Append( "\\\"" );
  1377. break;
  1378. case '\r':
  1379. sb.Append( "\\r" );
  1380. break;
  1381. case '\n':
  1382. sb.Append( "\\n" );
  1383. break;
  1384. case '\t':
  1385. sb.Append( "\\t" );
  1386. break;
  1387. default:
  1388. sb.Append( ch );
  1389. break;
  1390. }
  1391. }
  1392. i++;
  1393. }
  1394. if ( sb == null ) {
  1395. return value;
  1396. }
  1397. if ( i - start > 0 ) {
  1398. sb.Append( value, start, i - start );
  1399. }
  1400. return sb.ToString();
  1401. }
  1402. #endif
  1403. internal static Exception CreateException( string res, ExceptionType exceptionType ) {
  1404. return CreateException( res, exceptionType, 0, 0 );
  1405. }
  1406. internal static Exception CreateException( string res, ExceptionType exceptionType, int lineNo, int linePos ) {
  1407. switch ( exceptionType ) {
  1408. case ExceptionType.ArgumentException:
  1409. return new ArgumentException( Res.GetString( res ) );
  1410. case ExceptionType.XmlException:
  1411. default:
  1412. return new XmlException( res, string.Empty, lineNo, linePos );
  1413. }
  1414. }
  1415. internal static Exception CreateException( string res, string arg, ExceptionType exceptionType ) {
  1416. return CreateException( res, arg, exceptionType, 0, 0 );
  1417. }
  1418. internal static Exception CreateException( string res, string arg, ExceptionType exceptionType, int lineNo, int linePos ) {
  1419. switch ( exceptionType ) {
  1420. case ExceptionType.ArgumentException:
  1421. return new ArgumentException( Res.GetString( res, arg ) );
  1422. case ExceptionType.XmlException:
  1423. default:
  1424. return new XmlException( res, arg, lineNo, linePos );
  1425. }
  1426. }
  1427. internal static Exception CreateException( string res, string[] args, ExceptionType exceptionType ) {
  1428. return CreateException(res, args, exceptionType, 0, 0);
  1429. }
  1430. internal static Exception CreateException(string res, string[] args, ExceptionType exceptionType, int lineNo, int linePos) {
  1431. switch ( exceptionType ) {
  1432. case ExceptionType.ArgumentException:
  1433. return new ArgumentException( Res.GetString( res, args ) );
  1434. case ExceptionType.XmlException:
  1435. default:
  1436. return new XmlException( res, args, lineNo, linePos );
  1437. }
  1438. }
  1439. internal static Exception CreateInvalidSurrogatePairException( char low, char hi ) {
  1440. return CreateInvalidSurrogatePairException( low, hi, ExceptionType.ArgumentException );
  1441. }
  1442. internal static Exception CreateInvalidSurrogatePairException(char low, char hi, ExceptionType exceptionType) {
  1443. return CreateInvalidSurrogatePairException(low, hi, exceptionType, 0, 0);
  1444. }
  1445. internal static Exception CreateInvalidSurrogatePairException( char low, char hi, ExceptionType exceptionType, int lineNo, int linePos ) {
  1446. string[] args = new string[] {
  1447. ((uint)hi).ToString( "X", CultureInfo.InvariantCulture ),
  1448. ((uint)low).ToString( "X", CultureInfo.InvariantCulture )
  1449. } ;
  1450. return CreateException( Res.Xml_InvalidSurrogatePairWithArgs, args, exceptionType, lineNo, linePos );
  1451. }
  1452. internal static Exception CreateInvalidHighSurrogateCharException( char hi ) {
  1453. return CreateInvalidHighSurrogateCharException( hi, ExceptionType.ArgumentException );
  1454. }
  1455. internal static Exception CreateInvalidHighSurrogateCharException( char hi, ExceptionType exceptionType ) {
  1456. return CreateInvalidHighSurrogateCharException( hi, exceptionType, 0, 0);
  1457. }
  1458. internal static Exception CreateInvalidHighSurrogateCharException(char hi, ExceptionType exceptionType, int lineNo, int linePos) {
  1459. return CreateException( Res.Xml_InvalidSurrogateHighChar, ((uint)hi).ToString( "X", CultureInfo.InvariantCulture ), exceptionType, lineNo, linePos );
  1460. }
  1461. internal static Exception CreateInvalidCharException(char[] data, int length, int invCharPos) {
  1462. return CreateInvalidCharException(data, length, invCharPos, ExceptionType.ArgumentException);
  1463. }
  1464. internal static Exception CreateInvalidCharException(char[] data, int length, int invCharPos, ExceptionType exceptionType) {
  1465. return CreateException(Res.Xml_InvalidCharacter, XmlException.BuildCharExceptionArgs(data, length, invCharPos), exceptionType, 0, invCharPos + 1);
  1466. }
  1467. internal static Exception CreateInvalidCharException( string data, int invCharPos ) {
  1468. return CreateInvalidCharException( data, invCharPos, ExceptionType.ArgumentException );
  1469. }
  1470. internal static Exception CreateInvalidCharException( string data, int invCharPos, ExceptionType exceptionType ) {
  1471. return CreateException(Res.Xml_InvalidCharacter, XmlException.BuildCharExceptionArgs(data, invCharPos), exceptionType, 0, invCharPos + 1);
  1472. }
  1473. internal static Exception CreateInvalidCharException( char invChar, char nextChar ) {
  1474. return CreateInvalidCharException(invChar, nextChar, ExceptionType.ArgumentException);
  1475. }
  1476. internal static Exception CreateInvalidCharException( char invChar, char nextChar, ExceptionType exceptionType) {
  1477. return CreateException(Res.Xml_InvalidCharacter, XmlException.BuildCharExceptionArgs(invChar, nextChar), exceptionType);
  1478. }
  1479. internal static Exception CreateInvalidNameCharException(string name, int index, ExceptionType exceptionType) {
  1480. return CreateException(index == 0 ? Res.Xml_BadStartNameChar : Res.Xml_BadNameChar, XmlException.BuildCharExceptionArgs(name, index), exceptionType, 0, index + 1);
  1481. }
  1482. internal static ArgumentException CreateInvalidNameArgumentException(string name, string argumentName) {
  1483. return ( name == null ) ? new ArgumentNullException( argumentName ) : new ArgumentException( Res.GetString( Res.Xml_EmptyName ), argumentName );
  1484. }
  1485. }
  1486. }