XmlConvert.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. //
  2. // System.Xml.XmlConvert
  3. //
  4. // Authors:
  5. // Dwivedi, Ajay kumar ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Alan Tam Siu Lung ([email protected])
  8. // Atsushi Enomoto ([email protected])
  9. //
  10. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  11. //
  12. //
  13. // Permission is hereby granted, free of charge, to any person obtaining
  14. // a copy of this software and associated documentation files (the
  15. // "Software"), to deal in the Software without restriction, including
  16. // without limitation the rights to use, copy, modify, merge, publish,
  17. // distribute, sublicense, and/or sell copies of the Software, and to
  18. // permit persons to whom the Software is furnished to do so, subject to
  19. // the following conditions:
  20. //
  21. // The above copyright notice and this permission notice shall be
  22. // included in all copies or substantial portions of the Software.
  23. //
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  25. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  26. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  27. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  28. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  29. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  30. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  31. //
  32. using System;
  33. using System.IO;
  34. using System.Text;
  35. using System.Globalization;
  36. using System.Xml.Schema;
  37. namespace System.Xml {
  38. public class XmlConvert {
  39. const string encodedColon = "_x003A_";
  40. const NumberStyles floatStyle = NumberStyles.AllowCurrencySymbol |
  41. NumberStyles.AllowExponent |
  42. NumberStyles.AllowDecimalPoint |
  43. NumberStyles.AllowLeadingSign;
  44. static readonly string [] datetimeFormats = {
  45. // dateTime
  46. "yyyy-MM-ddTHH:mm:ss",
  47. "yyyy-MM-ddTHH:mm:ss.f",
  48. "yyyy-MM-ddTHH:mm:ss.ff",
  49. "yyyy-MM-ddTHH:mm:ss.fff",
  50. "yyyy-MM-ddTHH:mm:ss.ffff",
  51. "yyyy-MM-ddTHH:mm:ss.fffff",
  52. "yyyy-MM-ddTHH:mm:ss.ffffff",
  53. "yyyy-MM-ddTHH:mm:ss.fffffff",
  54. "yyyy-MM-ddTHH:mm:sszzz",
  55. "yyyy-MM-ddTHH:mm:ss.fzzz",
  56. "yyyy-MM-ddTHH:mm:ss.ffzzz",
  57. "yyyy-MM-ddTHH:mm:ss.fffzzz",
  58. "yyyy-MM-ddTHH:mm:ss.ffffzzz",
  59. "yyyy-MM-ddTHH:mm:ss.fffffzzz",
  60. "yyyy-MM-ddTHH:mm:ss.ffffffzzz",
  61. "yyyy-MM-ddTHH:mm:ss.fffffffzzz",
  62. "yyyy-MM-ddTHH:mm:ssZ",
  63. "yyyy-MM-ddTHH:mm:ss.fZ",
  64. "yyyy-MM-ddTHH:mm:ss.ffZ",
  65. "yyyy-MM-ddTHH:mm:ss.fffZ",
  66. "yyyy-MM-ddTHH:mm:ss.ffffZ",
  67. "yyyy-MM-ddTHH:mm:ss.fffffZ",
  68. "yyyy-MM-ddTHH:mm:ss.ffffffZ",
  69. "yyyy-MM-ddTHH:mm:ss.fffffffZ",
  70. // time
  71. "HH:mm:ss",
  72. "HH:mm:ss.f",
  73. "HH:mm:ss.ff",
  74. "HH:mm:ss.fff",
  75. "HH:mm:ss.ffff",
  76. "HH:mm:ss.fffff",
  77. "HH:mm:ss.ffffff",
  78. "HH:mm:ss.fffffff",
  79. "HH:mm:sszzz",
  80. "HH:mm:ss.fzzz",
  81. "HH:mm:ss.ffzzz",
  82. "HH:mm:ss.fffzzz",
  83. "HH:mm:ss.ffffzzz",
  84. "HH:mm:ss.fffffzzz",
  85. "HH:mm:ss.ffffffzzz",
  86. "HH:mm:ss.fffffffzzz",
  87. "HH:mm:ssZ",
  88. "HH:mm:ss.fZ",
  89. "HH:mm:ss.ffZ",
  90. "HH:mm:ss.fffZ",
  91. "HH:mm:ss.ffffZ",
  92. "HH:mm:ss.fffffZ",
  93. "HH:mm:ss.ffffffZ",
  94. "HH:mm:ss.fffffffZ",
  95. // date
  96. "yyyy-MM-dd",
  97. "yyyy-MM-ddzzz",
  98. "yyyy-MM-ddZ",
  99. // gYearMonth
  100. "yyyy-MM",
  101. "yyyy-MMzzz",
  102. "yyyy-MMZ",
  103. // gYear
  104. "yyyy",
  105. "yyyyzzz",
  106. "yyyyZ",
  107. // gMonthDay
  108. "--MM-dd",
  109. "--MM-ddzzz",
  110. "--MM-ddZ",
  111. // gDay
  112. "---dd",
  113. "---ddzzz",
  114. "---ddZ",
  115. };
  116. public XmlConvert()
  117. {}
  118. private static string TryDecoding (string s)
  119. {
  120. if (s == null || s.Length < 6)
  121. return s;
  122. char c = '\uFFFF';
  123. try {
  124. c = (char) Int32.Parse (s.Substring (1, 4), NumberStyles.HexNumber, CultureInfo.InvariantCulture);
  125. } catch {
  126. return s [0] + DecodeName (s.Substring (1));
  127. }
  128. if (s.Length == 6)
  129. return c.ToString ();
  130. return c + DecodeName (s.Substring (6));
  131. }
  132. public static string DecodeName (string name)
  133. {
  134. if (name == null || name.Length == 0)
  135. return name;
  136. int pos = name.IndexOf ('_');
  137. if (pos == -1 || pos + 6 >= name.Length)
  138. return name;
  139. if ((name [pos + 1] != 'X' && name [pos + 1] != 'x') || name [pos + 6] != '_')
  140. return name [0] + DecodeName (name.Substring (1));
  141. return name.Substring (0, pos) + TryDecoding (name.Substring (pos + 1));
  142. }
  143. public static string EncodeLocalName (string name)
  144. {
  145. if (name == null)
  146. return name;
  147. string encoded = EncodeName (name);
  148. int pos = encoded.IndexOf (':');
  149. if (pos == -1)
  150. return encoded;
  151. return encoded.Replace (":", encodedColon);
  152. }
  153. internal static bool IsInvalid (char c, bool firstOnlyLetter)
  154. {
  155. if (c == ':') // Special case. allowed in EncodeName, but encoded in EncodeLocalName
  156. return false;
  157. if (firstOnlyLetter)
  158. return !XmlChar.IsFirstNameChar (c);
  159. else
  160. return !XmlChar.IsNameChar (c);
  161. }
  162. private static string EncodeName (string name, bool nmtoken)
  163. {
  164. if (name == null || name.Length == 0)
  165. return name;
  166. StringBuilder sb = new StringBuilder ();
  167. int length = name.Length;
  168. for (int i = 0; i < length; i++) {
  169. char c = name [i];
  170. if (IsInvalid (c, i == 0 && !nmtoken))
  171. sb.AppendFormat ("_x{0:X4}_", (int) c);
  172. else if (c == '_' && i + 6 < length && name [i+1] == 'x' && name [i + 6] == '_')
  173. sb.Append ("_x005F_");
  174. else
  175. sb.Append (c);
  176. }
  177. return sb.ToString ();
  178. }
  179. public static string EncodeName (string name)
  180. {
  181. return EncodeName (name, false);
  182. }
  183. public static string EncodeNmToken (string name)
  184. {
  185. if (name == String.Empty)
  186. throw new XmlException ("Invalid NmToken: ''");
  187. return EncodeName (name, true);
  188. }
  189. // {true, false, 1, 0}
  190. public static bool ToBoolean(string s)
  191. {
  192. s = s.Trim (XmlChar.WhitespaceChars);
  193. switch(s)
  194. {
  195. case "1":
  196. return true;
  197. case "true":
  198. return true;
  199. case "0":
  200. return false;
  201. case "false":
  202. return false;
  203. default:
  204. throw new FormatException(s + " is not a valid boolean value");
  205. }
  206. }
  207. // LAMESPEC: It has been documented as public, but is marked as internal.
  208. internal static string ToBinHexString (byte [] buffer)
  209. {
  210. StringWriter w = new StringWriter ();
  211. WriteBinHex (buffer, 0, buffer.Length, w);
  212. return w.ToString ();
  213. }
  214. internal static void WriteBinHex (byte [] buffer, int index, int count, TextWriter w)
  215. {
  216. if (buffer == null)
  217. throw new ArgumentNullException ("buffer");
  218. if (index < 0)
  219. throw new ArgumentOutOfRangeException ("index", index, "index must be non negative integer.");
  220. if (count < 0)
  221. throw new ArgumentOutOfRangeException ("count", count, "count must be non negative integer.");
  222. if (buffer.Length < index + count)
  223. throw new ArgumentOutOfRangeException ("index and count must be smaller than the length of the buffer.");
  224. // Copied from XmlTextWriter.WriteBinHex ()
  225. int end = index + count;
  226. for (int i = index; i < end; i++) {
  227. int val = buffer [i];
  228. int high = val >> 4;
  229. int low = val & 15;
  230. if (high > 9)
  231. w.Write ((char) (high + 55));
  232. else
  233. w.Write ((char) (high + 0x30));
  234. if (low > 9)
  235. w.Write ((char) (low + 55));
  236. else
  237. w.Write ((char) (low + 0x30));
  238. }
  239. }
  240. public static byte ToByte(string s)
  241. {
  242. return Byte.Parse(s, CultureInfo.InvariantCulture);
  243. }
  244. public static char ToChar(string s)
  245. {
  246. return Char.Parse(s);
  247. }
  248. #if NET_2_0
  249. [Obsolete]
  250. #endif
  251. public static DateTime ToDateTime (string s)
  252. {
  253. return ToDateTime (s, datetimeFormats);
  254. }
  255. #if NET_2_0
  256. public static DateTime ToDateTime (string value, XmlDateTimeSerializationMode mode)
  257. {
  258. string modestr = null;
  259. switch (mode) {
  260. case XmlDateTimeSerializationMode.Local:
  261. case XmlDateTimeSerializationMode.RoundtripKind:
  262. return ToDateTime (value, "yyyy-MM-ddTHH:mm:ss.FFFFFFFzzz");
  263. case XmlDateTimeSerializationMode.Utc:
  264. return ToDateTime (value, "yyyy-MM-ddTHH:mm:ss.FFFFFFFZ").ToUniversalTime ();
  265. case XmlDateTimeSerializationMode.Unspecified:
  266. default:
  267. return ToDateTime (value, "yyyy-MM-ddTHH:mm:ss.FFFFFFF");
  268. }
  269. }
  270. #endif
  271. public static DateTime ToDateTime(string s, string format)
  272. {
  273. DateTimeFormatInfo d = new DateTimeFormatInfo();
  274. d.FullDateTimePattern = format;
  275. return DateTime.Parse(s, d);
  276. }
  277. public static DateTime ToDateTime(string s, string[] formats)
  278. {
  279. DateTimeStyles style = DateTimeStyles.AllowLeadingWhite |
  280. DateTimeStyles.AllowTrailingWhite;
  281. return DateTime.ParseExact (s, formats, DateTimeFormatInfo.InvariantInfo, style);
  282. }
  283. public static Decimal ToDecimal(string s)
  284. {
  285. return Decimal.Parse(s, CultureInfo.InvariantCulture);
  286. }
  287. public static double ToDouble(string s)
  288. {
  289. if (s == null)
  290. throw new ArgumentNullException();
  291. if (s == "INF")
  292. return Double.PositiveInfinity;
  293. if (s == "-INF")
  294. return Double.NegativeInfinity;
  295. if (s == "NaN")
  296. return Double.NaN;
  297. return Double.Parse (s, floatStyle, CultureInfo.InvariantCulture);
  298. }
  299. public static Guid ToGuid(string s)
  300. {
  301. return new Guid(s);
  302. }
  303. public static short ToInt16(string s)
  304. {
  305. return Int16.Parse(s, CultureInfo.InvariantCulture);
  306. }
  307. public static int ToInt32(string s)
  308. {
  309. return Int32.Parse(s, CultureInfo.InvariantCulture);
  310. }
  311. public static long ToInt64(string s)
  312. {
  313. return Int64.Parse(s, CultureInfo.InvariantCulture);
  314. }
  315. [CLSCompliant (false)]
  316. public static SByte ToSByte(string s)
  317. {
  318. return SByte.Parse(s, CultureInfo.InvariantCulture);
  319. }
  320. public static float ToSingle(string s)
  321. {
  322. if (s == null)
  323. throw new ArgumentNullException();
  324. if (s == "INF")
  325. return Single.PositiveInfinity;
  326. if (s == "-INF")
  327. return Single.NegativeInfinity;
  328. if (s == "NaN")
  329. return Single.NaN;
  330. return Single.Parse(s, floatStyle, CultureInfo.InvariantCulture);
  331. }
  332. public static string ToString(Guid value)
  333. {
  334. return value.ToString("D", CultureInfo.InvariantCulture);
  335. }
  336. public static string ToString(int value)
  337. {
  338. return value.ToString(CultureInfo.InvariantCulture);
  339. }
  340. public static string ToString(short value)
  341. {
  342. return value.ToString(CultureInfo.InvariantCulture);
  343. }
  344. public static string ToString(byte value)
  345. {
  346. return value.ToString(CultureInfo.InvariantCulture);
  347. }
  348. public static string ToString(long value)
  349. {
  350. return value.ToString(CultureInfo.InvariantCulture);
  351. }
  352. public static string ToString(char value)
  353. {
  354. return value.ToString(CultureInfo.InvariantCulture);
  355. }
  356. public static string ToString(bool value)
  357. {
  358. if (value) return "true";
  359. return "false";
  360. }
  361. [CLSCompliant (false)]
  362. public static string ToString(SByte value)
  363. {
  364. return value.ToString(CultureInfo.InvariantCulture);
  365. }
  366. public static string ToString(Decimal value)
  367. {
  368. return value.ToString (CultureInfo.InvariantCulture);
  369. }
  370. [CLSCompliant (false)]
  371. public static string ToString(UInt64 value)
  372. {
  373. return value.ToString(CultureInfo.InvariantCulture);
  374. }
  375. public static string ToString (TimeSpan value)
  376. {
  377. if (value == TimeSpan.Zero)
  378. return "PT0S";
  379. StringBuilder builder = new StringBuilder ();
  380. if (value.Ticks < 0) {
  381. builder.Append ('-');
  382. value = value.Negate ();
  383. }
  384. builder.Append ('P');
  385. if (value.Days > 0)
  386. builder.Append (value.Days).Append ('D');
  387. if (value.Days > 0 || value.Hours > 0 || value.Minutes > 0 || value.Seconds > 0 || value.Milliseconds > 0) {
  388. builder.Append('T');
  389. if (value.Hours > 0)
  390. builder.Append (value.Hours).Append ('H');
  391. if (value.Minutes > 0)
  392. builder.Append (value.Minutes).Append ('M');
  393. if (value.Seconds > 0 || value.Milliseconds > 0) {
  394. builder.Append (value.Seconds);
  395. long ticks = value.Ticks % TimeSpan.TicksPerMillisecond;
  396. if (ticks > 0)
  397. builder.Append ('.').AppendFormat ("{0:0000000}", value.Ticks % TimeSpan.TicksPerSecond);
  398. else if (value.Milliseconds > 0)
  399. builder.Append ('.').AppendFormat ("{0:000}", value.Milliseconds);
  400. builder.Append ('S');
  401. }
  402. }
  403. return builder.ToString ();
  404. }
  405. public static string ToString(double value)
  406. {
  407. if (Double.IsNegativeInfinity(value)) return "-INF";
  408. if (Double.IsPositiveInfinity(value)) return "INF";
  409. if (Double.IsNaN(value)) return "NaN";
  410. return value.ToString(CultureInfo.InvariantCulture);
  411. }
  412. public static string ToString(float value)
  413. {
  414. if (Single.IsNegativeInfinity(value)) return "-INF";
  415. if (Single.IsPositiveInfinity(value)) return "INF";
  416. if (Single.IsNaN(value)) return "NaN";
  417. return value.ToString(CultureInfo.InvariantCulture);
  418. }
  419. [CLSCompliant (false)]
  420. public static string ToString(UInt32 value)
  421. {
  422. return value.ToString(CultureInfo.InvariantCulture);
  423. }
  424. [CLSCompliant (false)]
  425. public static string ToString(UInt16 value)
  426. {
  427. return value.ToString(CultureInfo.InvariantCulture);
  428. }
  429. #if NET_2_0
  430. [Obsolete]
  431. #endif
  432. public static string ToString (DateTime value)
  433. {
  434. return value.ToString ("yyyy-MM-ddTHH:mm:ss.fffffffzzz", CultureInfo.InvariantCulture);
  435. }
  436. #if NET_2_0
  437. public static string ToString (DateTime value, XmlDateTimeSerializationMode mode)
  438. {
  439. // Unlike usual DateTime formatting, it preserves
  440. // MaxValue/MinValue as is.
  441. string modestr = null;
  442. switch (mode) {
  443. case XmlDateTimeSerializationMode.Local:
  444. return (value == DateTime.MinValue ? DateTime.MinValue : value == DateTime.MaxValue ? value : value.ToLocalTime ()).ToString (
  445. "yyyy-MM-ddTHH:mm:ss.FFFFFFFzzz",
  446. CultureInfo.InvariantCulture);
  447. break;
  448. case XmlDateTimeSerializationMode.RoundtripKind:
  449. return value.ToString (
  450. "yyyy-MM-ddTHH:mm:ss.FFFFFFF",
  451. CultureInfo.InvariantCulture);
  452. break;
  453. default:
  454. return value.ToString (
  455. "yyyy-MM-ddTHH:mm:ss.FFFFFFFzzz",
  456. CultureInfo.InvariantCulture);
  457. break;
  458. case XmlDateTimeSerializationMode.Utc:
  459. return (value == DateTime.MinValue ? DateTime.MinValue : value == DateTime.MaxValue ? value : value.ToUniversalTime ()).ToString (
  460. "yyyy-MM-ddTHH:mm:ss.FFFFFFFZ",
  461. CultureInfo.InvariantCulture);
  462. break;
  463. case XmlDateTimeSerializationMode.Unspecified:
  464. return value.ToString (
  465. "yyyy-MM-ddTHH:mm:ss.FFFFFFF",
  466. CultureInfo.InvariantCulture);
  467. break;
  468. }
  469. }
  470. #endif
  471. public static string ToString(DateTime value, string format)
  472. {
  473. return value.ToString(format, CultureInfo.InvariantCulture);
  474. }
  475. public static TimeSpan ToTimeSpan(string s)
  476. {
  477. if (s.Length == 0)
  478. throw new ArgumentException ("Invalid format string for duration schema datatype.");
  479. int start = 0;
  480. if (s [0] == '-')
  481. start = 1;
  482. bool minusValue = (start == 1);
  483. if (s [start] != 'P')
  484. throw new ArgumentException ("Invalid format string for duration schema datatype.");
  485. start++;
  486. int parseStep = 0;
  487. int days = 0;
  488. bool isTime = false;
  489. int hours = 0;
  490. int minutes = 0;
  491. int seconds = 0;
  492. long ticks = 0;
  493. int parsedDigits = 0;
  494. bool error = false;
  495. int i = start;
  496. while (i < s.Length) {
  497. if (s [i] == 'T') {
  498. isTime = true;
  499. parseStep = 4;
  500. i++;
  501. start = i;
  502. continue;
  503. }
  504. for (; i < s.Length; i++)
  505. if (s [i] < '0' || '9' < s [i])
  506. break;
  507. if (parseStep == 7)
  508. parsedDigits = i - start;
  509. int value = int.Parse (s.Substring (start, i - start), CultureInfo.InvariantCulture);
  510. if (parseStep == 7) {
  511. // adjust to 7 digits so that it makes sense as millisecond digits
  512. for (; parsedDigits > 7; parsedDigits--)
  513. value /= 10;
  514. for (; parsedDigits < 7; parsedDigits++)
  515. value *= 10;
  516. }
  517. switch (s [i]) {
  518. case 'Y':
  519. days += value * 365;
  520. if (parseStep > 0)
  521. error = true;
  522. else
  523. parseStep = 1;
  524. break;
  525. case 'M':
  526. if (parseStep < 2) {
  527. days += 365 * (value / 12) + 30 * (value % 12);
  528. parseStep = 2;
  529. } else if (isTime && parseStep < 6) {
  530. minutes = value;
  531. parseStep = 6;
  532. }
  533. else
  534. error = true;
  535. break;
  536. case 'D':
  537. days += value;
  538. if (parseStep > 2)
  539. error = true;
  540. else
  541. parseStep = 3;
  542. break;
  543. case 'H':
  544. hours = value;
  545. if (!isTime || parseStep > 4)
  546. error = true;
  547. else
  548. parseStep = 5;
  549. break;
  550. case 'S':
  551. if (parseStep == 7)
  552. ticks = value;
  553. else
  554. seconds = value;
  555. if (!isTime || parseStep > 7)
  556. error = true;
  557. else
  558. parseStep = 8;
  559. break;
  560. case '.':
  561. if (parseStep > 7)
  562. error = true;
  563. seconds = value;
  564. parseStep = 7;
  565. break;
  566. default:
  567. error = true;
  568. break;
  569. }
  570. if (error)
  571. break;
  572. ++i;
  573. start = i;
  574. }
  575. if (error)
  576. throw new ArgumentException ("Invalid format string for duration schema datatype.");
  577. TimeSpan ts = new TimeSpan (days, hours, minutes, seconds);
  578. if (minusValue)
  579. return TimeSpan.FromTicks (- (ts.Ticks + ticks));
  580. else
  581. return TimeSpan.FromTicks (ts.Ticks + ticks);
  582. }
  583. [CLSCompliant (false)]
  584. public static UInt16 ToUInt16(string s)
  585. {
  586. return UInt16.Parse(s, CultureInfo.InvariantCulture);
  587. }
  588. [CLSCompliant (false)]
  589. public static UInt32 ToUInt32(string s)
  590. {
  591. return UInt32.Parse(s, CultureInfo.InvariantCulture);
  592. }
  593. [CLSCompliant (false)]
  594. public static UInt64 ToUInt64(string s)
  595. {
  596. return UInt64.Parse(s, CultureInfo.InvariantCulture);
  597. }
  598. public static string VerifyName (string name)
  599. {
  600. if (name == null || name.Length == 0)
  601. throw new ArgumentNullException("name");
  602. if (!XmlChar.IsName (name))
  603. throw new XmlException("'" + name + "' is not a valid XML Name");
  604. return name;
  605. }
  606. public static string VerifyNCName (string ncname)
  607. {
  608. if (ncname == null || ncname.Length == 0)
  609. throw new ArgumentNullException("ncname");
  610. if (!XmlChar.IsNCName (ncname))
  611. throw new XmlException ("'" + ncname + "' is not a valid XML NCName");
  612. return ncname;
  613. }
  614. #if NET_2_0
  615. public static string VerifyTOKEN (string name)
  616. #else
  617. internal static string VerifyTOKEN (string name)
  618. #endif
  619. {
  620. if (name == null)
  621. throw new ArgumentNullException("name");
  622. if (name.Length == 0)
  623. return name;
  624. if (XmlChar.IsWhitespace (name [0]) ||
  625. XmlChar.IsWhitespace (name [name.Length - 1]))
  626. throw new XmlException ("Whitespace characters (#xA, #xD, #x9, #x20) are not allowed as leading or trailing whitespaces of xs:token.");
  627. for (int i = 0; i < name.Length; i++)
  628. if (XmlChar.IsWhitespace (name [i]) && name [i] != ' ')
  629. throw new XmlException ("Either #xA, #xD or #x9 are not allowed inside xs:token.");
  630. return name;
  631. }
  632. #if NET_2_0
  633. public static string VerifyNMTOKEN (string name)
  634. #else
  635. internal static string VerifyNMTOKEN (string name)
  636. #endif
  637. {
  638. if (name == null)
  639. throw new ArgumentNullException("name");
  640. if (!XmlChar.IsNmToken (name))
  641. throw new XmlException("'" + name + "' is not a valid XML NMTOKEN");
  642. return name;
  643. }
  644. // It is documented as public method, but in fact it is not.
  645. internal static byte [] FromBinHexString (string s)
  646. {
  647. char [] chars = s.ToCharArray ();
  648. byte [] bytes = new byte [chars.Length / 2 + chars.Length % 2];
  649. FromBinHexString (chars, 0, chars.Length, bytes);
  650. return bytes;
  651. }
  652. internal static int FromBinHexString (char [] chars, int offset, int charLength, byte [] buffer)
  653. {
  654. int bufIndex = offset;
  655. for (int i = 0; i < charLength - 1; i += 2) {
  656. buffer [bufIndex] = (chars [i] > '9' ?
  657. (byte) (chars [i] - 'A' + 10) :
  658. (byte) (chars [i] - '0'));
  659. buffer [bufIndex] <<= 4;
  660. buffer [bufIndex] += chars [i + 1] > '9' ?
  661. (byte) (chars [i + 1] - 'A' + 10) :
  662. (byte) (chars [i + 1] - '0');
  663. bufIndex++;
  664. }
  665. if (charLength %2 != 0)
  666. buffer [bufIndex++] = (byte)
  667. ((chars [charLength - 1] > '9' ?
  668. (byte) (chars [charLength - 1] - 'A' + 10) :
  669. (byte) (chars [charLength - 1] - '0'))
  670. << 4);
  671. return bufIndex - offset;
  672. }
  673. }
  674. }