Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
XmlConvert.cs
Go to the documentation of this file.
4using System.Text;
7
8namespace System.Xml;
9
10public class XmlConvert
11{
13
14 internal static char[] crt = new char[3] { '\n', '\r', '\t' };
15
16 private static volatile Regex s_encodeCharPattern;
17
18 private static volatile Regex s_decodeCharPattern;
19
20 private static volatile string[] s_allDateTimeFormats;
21
22 internal static readonly char[] WhitespaceChars = new char[4] { ' ', '\t', '\n', '\r' };
23
24 private static string[] AllDateTimeFormats
25 {
26 get
27 {
28 if (s_allDateTimeFormats == null)
29 {
31 }
33 }
34 }
35
36 [return: NotNullIfNotNull("name")]
37 public static string? EncodeName(string? name)
38 {
39 return EncodeName(name, first: true, local: false);
40 }
41
42 [return: NotNullIfNotNull("name")]
43 public static string? EncodeNmToken(string? name)
44 {
45 return EncodeName(name, first: false, local: false);
46 }
47
48 [return: NotNullIfNotNull("name")]
49 public static string? EncodeLocalName(string? name)
50 {
51 return EncodeName(name, first: true, local: true);
52 }
53
54 [return: NotNullIfNotNull("name")]
55 public static string? DecodeName(string? name)
56 {
57 if (string.IsNullOrEmpty(name))
58 {
59 return name;
60 }
62 int length = name.Length;
63 int num = 0;
64 int num2 = name.IndexOf('_');
67 if (num2 >= 0)
68 {
69 if (s_decodeCharPattern == null)
70 {
71 s_decodeCharPattern = new Regex("_[Xx]([0-9a-fA-F]{4}|[0-9a-fA-F]{8})_");
72 }
74 enumerator = matchCollection.GetEnumerator();
75 int num3 = -1;
76 if (enumerator != null && enumerator.MoveNext())
77 {
78 Match match = (Match)enumerator.Current;
79 num3 = match.Index;
80 }
81 for (int i = 0; i < length - 7 + 1; i++)
82 {
83 if (i != num3)
84 {
85 continue;
86 }
87 if (enumerator.MoveNext())
88 {
89 Match match2 = (Match)enumerator.Current;
90 num3 = match2.Index;
91 }
92 if (stringBuilder == null)
93 {
95 }
96 stringBuilder.Append(name, num, i - num);
97 if (name[i + 6] != '_')
98 {
99 int num4 = FromHex(name[i + 2]) * 268435456 + FromHex(name[i + 3]) * 16777216 + FromHex(name[i + 4]) * 1048576 + FromHex(name[i + 5]) * 65536 + FromHex(name[i + 6]) * 4096 + FromHex(name[i + 7]) * 256 + FromHex(name[i + 8]) * 16 + FromHex(name[i + 9]);
100 if (num4 >= 65536)
101 {
102 if (num4 <= 1114111)
103 {
104 num = i + 7 + 4;
106 stringBuilder.Append(highChar);
107 stringBuilder.Append(lowChar);
108 }
109 }
110 else
111 {
112 num = i + 7 + 4;
113 stringBuilder.Append((char)num4);
114 }
115 i += 10;
116 }
117 else
118 {
119 num = i + 7;
120 stringBuilder.Append((char)(FromHex(name[i + 2]) * 4096 + FromHex(name[i + 3]) * 256 + FromHex(name[i + 4]) * 16 + FromHex(name[i + 5])));
121 i += 6;
122 }
123 }
124 if (num == 0)
125 {
126 return name;
127 }
128 if (num < length)
129 {
130 stringBuilder.Append(name, num, length - num);
131 }
132 return stringBuilder.ToString();
133 }
134 return name;
135 }
136
137 [return: NotNullIfNotNull("name")]
138 private static string EncodeName(string name, bool first, bool local)
139 {
140 if (string.IsNullOrEmpty(name))
141 {
142 return name;
143 }
145 int length = name.Length;
146 int num = 0;
147 int i = 0;
148 int num2 = name.IndexOf('_');
150 IEnumerator enumerator = null;
151 if (num2 >= 0)
152 {
153 if (s_encodeCharPattern == null)
154 {
155 s_encodeCharPattern = new Regex("(?<=_)[Xx]([0-9a-fA-F]{4}|[0-9a-fA-F]{8})_");
156 }
158 enumerator = matchCollection.GetEnumerator();
159 }
160 int num3 = -1;
161 if (enumerator != null && enumerator.MoveNext())
162 {
163 Match match = (Match)enumerator.Current;
164 num3 = match.Index - 1;
165 }
166 if (first && ((!XmlCharType.IsStartNCNameCharXml4e(name[0]) && (local || (!local && name[0] != ':'))) || num3 == 0))
167 {
168 if (stringBuilder == null)
169 {
171 }
172 stringBuilder.Append("_x");
174 {
175 int highChar = name[0];
176 int lowChar = name[1];
181 handler.AppendFormatted(value, "X8");
182 stringBuilder3.Append(ref handler);
183 i++;
184 num = 2;
185 }
186 else
187 {
191 handler2.AppendFormatted((int)name[0], "X4");
193 num = 1;
194 }
195 stringBuilder.Append('_');
196 i++;
197 if (num3 == 0 && enumerator.MoveNext())
198 {
199 Match match2 = (Match)enumerator.Current;
200 num3 = match2.Index - 1;
201 }
202 }
203 for (; i < length; i++)
204 {
206 {
207 if (stringBuilder == null)
208 {
210 }
211 if (num3 == i && enumerator.MoveNext())
212 {
213 Match match3 = (Match)enumerator.Current;
214 num3 = match3.Index - 1;
215 }
216 stringBuilder.Append(name, num, i - num);
217 stringBuilder.Append("_x");
219 {
220 int highChar2 = name[i];
221 int lowChar2 = name[i + 1];
228 num = i + 2;
229 i++;
230 }
231 else
232 {
236 handler4.AppendFormatted((int)name[i], "X4");
238 num = i + 1;
239 }
240 stringBuilder.Append('_');
241 }
242 }
243 if (num == 0)
244 {
245 return name;
246 }
247 if (num < length)
248 {
249 stringBuilder.Append(name, num, length - num);
250 }
251 return stringBuilder.ToString();
252 }
253
254 private static int FromHex(char digit)
255 {
256 return System.HexConverter.FromChar(digit);
257 }
258
259 internal static byte[] FromBinHexString(string s)
260 {
261 return FromBinHexString(s, allowOddCount: true);
262 }
263
264 internal static byte[] FromBinHexString(string s, bool allowOddCount)
265 {
266 if (s == null)
267 {
268 throw new ArgumentNullException("s");
269 }
270 return BinHexDecoder.Decode(s.ToCharArray(), allowOddCount);
271 }
272
273 internal static string ToBinHexString(byte[] inArray)
274 {
275 if (inArray == null)
276 {
277 throw new ArgumentNullException("inArray");
278 }
279 return BinHexEncoder.Encode(inArray, 0, inArray.Length);
280 }
281
282 public static string VerifyName(string name)
283 {
284 if (name == null)
285 {
286 throw new ArgumentNullException("name");
287 }
288 if (name.Length == 0)
289 {
290 throw new ArgumentNullException("name", System.SR.Xml_EmptyName);
291 }
293 if (num != name.Length)
294 {
295 throw CreateInvalidNameCharException(name, num, ExceptionType.XmlException);
296 }
297 return name;
298 }
299
300 internal static Exception TryVerifyName(string name)
301 {
302 if (string.IsNullOrEmpty(name))
303 {
304 return new XmlException(System.SR.Xml_EmptyName, string.Empty);
305 }
307 if (num != name.Length)
308 {
310 }
311 return null;
312 }
313
314 internal static string VerifyQName(string name, ExceptionType exceptionType)
315 {
316 if (string.IsNullOrEmpty(name))
317 {
318 throw new ArgumentNullException("name");
319 }
320 int colonOffset = -1;
322 if (num != name.Length)
323 {
325 }
326 return name;
327 }
328
329 public static string VerifyNCName(string name)
330 {
331 return VerifyNCName(name, ExceptionType.XmlException);
332 }
333
334 internal static string VerifyNCName(string name, ExceptionType exceptionType)
335 {
336 if (name == null)
337 {
338 throw new ArgumentNullException("name");
339 }
340 if (name.Length == 0)
341 {
343 }
344 int num = ValidateNames.ParseNCName(name, 0);
345 if (num != name.Length)
346 {
348 }
349 return name;
350 }
351
352 internal static Exception TryVerifyNCName(string name)
353 {
354 int num = ValidateNames.ParseNCName(name);
355 if (num == 0 || num != name.Length)
356 {
358 }
359 return null;
360 }
361
362 [return: NotNullIfNotNull("token")]
363 public static string? VerifyTOKEN(string? token)
364 {
365 if (string.IsNullOrEmpty(token))
366 {
367 return token;
368 }
369 if (token[0] == ' ' || token[token.Length - 1] == ' ' || token.IndexOfAny(crt) != -1 || token.IndexOf(" ", StringComparison.Ordinal) != -1)
370 {
371 throw new XmlException(System.SR.Sch_NotTokenString, token);
372 }
373 return token;
374 }
375
376 internal static Exception TryVerifyTOKEN(string token)
377 {
378 if (token == null || token.Length == 0)
379 {
380 return null;
381 }
382 if (token[0] == ' ' || token[token.Length - 1] == ' ' || token.IndexOfAny(crt) != -1 || token.IndexOf(" ", StringComparison.Ordinal) != -1)
383 {
384 return new XmlException(System.SR.Sch_NotTokenString, token);
385 }
386 return null;
387 }
388
389 public static string VerifyNMTOKEN(string name)
390 {
391 return VerifyNMTOKEN(name, ExceptionType.XmlException);
392 }
393
394 internal static string VerifyNMTOKEN(string name, ExceptionType exceptionType)
395 {
396 if (name == null)
397 {
398 throw new ArgumentNullException("name");
399 }
400 if (name.Length == 0)
401 {
403 }
405 if (num != name.Length)
406 {
408 }
409 return name;
410 }
411
412 internal static Exception TryVerifyNMTOKEN(string name)
413 {
414 if (name == null || name.Length == 0)
415 {
416 return new XmlException(System.SR.Xml_EmptyName, string.Empty);
417 }
419 if (num != name.Length)
420 {
422 }
423 return null;
424 }
425
427 {
428 if (str.IndexOfAny(crt) != -1)
429 {
431 }
432 return null;
433 }
434
435 public static string VerifyXmlChars(string content)
436 {
437 if (content == null)
438 {
439 throw new ArgumentNullException("content");
440 }
441 VerifyCharData(content, ExceptionType.XmlException);
442 return content;
443 }
444
445 public static string VerifyPublicId(string publicId)
446 {
447 if (publicId == null)
448 {
449 throw new ArgumentNullException("publicId");
450 }
451 int num = XmlCharType.IsPublicId(publicId);
452 if (num != -1)
453 {
454 throw CreateInvalidCharException(publicId, num, ExceptionType.XmlException);
455 }
456 return publicId;
457 }
458
459 public static string VerifyWhitespace(string content)
460 {
461 if (content == null)
462 {
463 throw new ArgumentNullException("content");
464 }
465 int num = XmlCharType.IsOnlyWhitespaceWithPos(content);
466 if (num != -1)
467 {
469 }
470 return content;
471 }
472
473 public static bool IsStartNCNameChar(char ch)
474 {
476 }
477
478 public static bool IsNCNameChar(char ch)
479 {
481 }
482
483 public static bool IsXmlChar(char ch)
484 {
485 return XmlCharType.IsCharData(ch);
486 }
487
488 public static bool IsXmlSurrogatePair(char lowChar, char highChar)
489 {
491 {
493 }
494 return false;
495 }
496
497 public static bool IsPublicIdChar(char ch)
498 {
499 return XmlCharType.IsPubidChar(ch);
500 }
501
502 public static bool IsWhitespaceChar(char ch)
503 {
505 }
506
507 public static string ToString(bool value)
508 {
509 if (!value)
510 {
511 return "false";
512 }
513 return "true";
514 }
515
516 public static string ToString(char value)
517 {
518 return value.ToString();
519 }
520
521 public static string ToString(decimal value)
522 {
523 return value.ToString(null, NumberFormatInfo.InvariantInfo);
524 }
525
526 [CLSCompliant(false)]
527 public static string ToString(sbyte value)
528 {
529 return value.ToString(null, s_invariantCultureInfo);
530 }
531
532 public static string ToString(short value)
533 {
534 return value.ToString(null, s_invariantCultureInfo);
535 }
536
537 public static string ToString(int value)
538 {
539 return value.ToString(null, s_invariantCultureInfo);
540 }
541
542 public static string ToString(long value)
543 {
544 return value.ToString(null, s_invariantCultureInfo);
545 }
546
547 public static string ToString(byte value)
548 {
549 return value.ToString(null, s_invariantCultureInfo);
550 }
551
552 [CLSCompliant(false)]
553 public static string ToString(ushort value)
554 {
555 return value.ToString(null, s_invariantCultureInfo);
556 }
557
558 [CLSCompliant(false)]
559 public static string ToString(uint value)
560 {
561 return value.ToString(null, s_invariantCultureInfo);
562 }
563
564 [CLSCompliant(false)]
565 public static string ToString(ulong value)
566 {
567 return value.ToString(null, s_invariantCultureInfo);
568 }
569
570 public static string ToString(float value)
571 {
572 if (float.IsNegativeInfinity(value))
573 {
574 return "-INF";
575 }
576 if (float.IsPositiveInfinity(value))
577 {
578 return "INF";
579 }
581 {
582 return "-0";
583 }
584 return value.ToString("R", NumberFormatInfo.InvariantInfo);
585 }
586
587 public static string ToString(double value)
588 {
589 if (double.IsNegativeInfinity(value))
590 {
591 return "-INF";
592 }
593 if (double.IsPositiveInfinity(value))
594 {
595 return "INF";
596 }
598 {
599 return "-0";
600 }
601 return value.ToString("R", NumberFormatInfo.InvariantInfo);
602 }
603
604 public static string ToString(TimeSpan value)
605 {
606 return new XsdDuration(value).ToString();
607 }
608
609 [Obsolete("Use XmlConvert.ToString() that accepts an XmlDateTimeSerializationMode instead.")]
610 public static string ToString(DateTime value)
611 {
612 return ToString(value, "yyyy-MM-ddTHH:mm:ss.fffffffzzzzzz");
613 }
614
615 public static string ToString(DateTime value, string format)
616 {
618 }
619
621 {
622 switch (dateTimeOption)
623 {
626 break;
629 break;
630 case XmlDateTimeSerializationMode.Unspecified:
631 value = new DateTime(value.Ticks, DateTimeKind.Unspecified);
632 break;
633 default:
635 case XmlDateTimeSerializationMode.RoundtripKind:
636 break;
637 }
638 return new XsdDateTime(value, XsdDateTimeFlags.DateTime).ToString();
639 }
640
641 public static string ToString(DateTimeOffset value)
642 {
643 return new XsdDateTime(value).ToString();
644 }
645
646 public static string ToString(DateTimeOffset value, string format)
647 {
649 }
650
651 public static string ToString(Guid value)
652 {
653 return value.ToString();
654 }
655
656 public static bool ToBoolean(string s)
657 {
658 s = TrimString(s);
659 switch (s)
660 {
661 case "1":
662 case "true":
663 return true;
664 case "0":
665 case "false":
666 return false;
667 default:
669 }
670 }
671
672 internal static Exception TryToBoolean(string s, out bool result)
673 {
674 s = TrimString(s);
675 switch (s)
676 {
677 case "0":
678 case "false":
679 result = false;
680 return null;
681 case "1":
682 case "true":
683 result = true;
684 return null;
685 default:
686 result = false;
688 }
689 }
690
691 public static char ToChar(string s)
692 {
693 if (s == null)
694 {
695 throw new ArgumentNullException("s");
696 }
697 if (s.Length != 1)
698 {
700 }
701 return s[0];
702 }
703
704 internal static Exception TryToChar(string s, out char result)
705 {
706 if (!char.TryParse(s, out result))
707 {
709 }
710 return null;
711 }
712
713 public static decimal ToDecimal(string s)
714 {
715 return decimal.Parse(s, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo);
716 }
717
718 internal static Exception TryToDecimal(string s, out decimal result)
719 {
720 if (!decimal.TryParse(s, NumberStyles.Integer | NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo, out result))
721 {
723 }
724 return null;
725 }
726
727 internal static decimal ToInteger(string s)
728 {
729 return decimal.Parse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
730 }
731
732 internal static Exception TryToInteger(string s, out decimal result)
733 {
734 if (!decimal.TryParse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out result))
735 {
737 }
738 return null;
739 }
740
741 [CLSCompliant(false)]
742 public static sbyte ToSByte(string s)
743 {
744 return sbyte.Parse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
745 }
746
747 internal static Exception TryToSByte(string s, out sbyte result)
748 {
749 if (!sbyte.TryParse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out result))
750 {
752 }
753 return null;
754 }
755
756 public static short ToInt16(string s)
757 {
758 return short.Parse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
759 }
760
761 internal static Exception TryToInt16(string s, out short result)
762 {
763 if (!short.TryParse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out result))
764 {
766 }
767 return null;
768 }
769
770 public static int ToInt32(string s)
771 {
772 return int.Parse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
773 }
774
775 internal static Exception TryToInt32(string s, out int result)
776 {
777 if (!int.TryParse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out result))
778 {
780 }
781 return null;
782 }
783
784 public static long ToInt64(string s)
785 {
786 return long.Parse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo);
787 }
788
789 internal static Exception TryToInt64(string s, out long result)
790 {
791 if (!long.TryParse(s, NumberStyles.Integer, NumberFormatInfo.InvariantInfo, out result))
792 {
794 }
795 return null;
796 }
797
798 public static byte ToByte(string s)
799 {
800 return byte.Parse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
801 }
802
803 internal static Exception TryToByte(string s, out byte result)
804 {
805 if (!byte.TryParse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result))
806 {
808 }
809 return null;
810 }
811
812 [CLSCompliant(false)]
813 public static ushort ToUInt16(string s)
814 {
815 return ushort.Parse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
816 }
817
818 internal static Exception TryToUInt16(string s, out ushort result)
819 {
820 if (!ushort.TryParse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result))
821 {
823 }
824 return null;
825 }
826
827 [CLSCompliant(false)]
828 public static uint ToUInt32(string s)
829 {
830 return uint.Parse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
831 }
832
833 internal static Exception TryToUInt32(string s, out uint result)
834 {
835 if (!uint.TryParse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result))
836 {
838 }
839 return null;
840 }
841
842 [CLSCompliant(false)]
843 public static ulong ToUInt64(string s)
844 {
845 return ulong.Parse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo);
846 }
847
848 internal static Exception TryToUInt64(string s, out ulong result)
849 {
850 if (!ulong.TryParse(s, NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite, NumberFormatInfo.InvariantInfo, out result))
851 {
853 }
854 return null;
855 }
856
857 public static float ToSingle(string s)
858 {
859 s = TrimString(s);
860 if (s == "-INF")
861 {
862 return float.NegativeInfinity;
863 }
864 if (s == "INF")
865 {
866 return float.PositiveInfinity;
867 }
868 float num = float.Parse(s, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo);
869 if (num == 0f && s[0] == '-')
870 {
871 return -0f;
872 }
873 return num;
874 }
875
876 internal static Exception TryToSingle(string s, out float result)
877 {
878 s = TrimString(s);
879 if (s == "-INF")
880 {
881 result = float.NegativeInfinity;
882 return null;
883 }
884 if (s == "INF")
885 {
886 result = float.PositiveInfinity;
887 return null;
888 }
889 if (!float.TryParse(s, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo, out result))
890 {
892 }
893 if (result == 0f && s[0] == '-')
894 {
895 result = -0f;
896 }
897 return null;
898 }
899
900 public static double ToDouble(string s)
901 {
902 s = TrimString(s);
903 if (s == "-INF")
904 {
905 return double.NegativeInfinity;
906 }
907 if (s == "INF")
908 {
909 return double.PositiveInfinity;
910 }
911 double num = double.Parse(s, NumberStyles.Float, NumberFormatInfo.InvariantInfo);
912 if (num == 0.0 && s[0] == '-')
913 {
914 return -0.0;
915 }
916 return num;
917 }
918
919 internal static Exception TryToDouble(string s, out double result)
920 {
921 s = TrimString(s);
922 if (s == "-INF")
923 {
924 result = double.NegativeInfinity;
925 return null;
926 }
927 if (s == "INF")
928 {
929 result = double.PositiveInfinity;
930 return null;
931 }
932 if (!double.TryParse(s, NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint | NumberStyles.AllowExponent, NumberFormatInfo.InvariantInfo, out result))
933 {
935 }
936 if (result == 0.0 && s[0] == '-')
937 {
938 result = -0.0;
939 }
940 return null;
941 }
942
943 internal static double ToXPathDouble(object o)
944 {
945 if (o is string value)
946 {
947 string text = TrimString(value);
948 if (text.Length != 0 && text[0] != '+' && double.TryParse(text, NumberStyles.AllowTrailingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowDecimalPoint, NumberFormatInfo.InvariantInfo, out var result))
949 {
950 return result;
951 }
952 return double.NaN;
953 }
954 if (o is double)
955 {
956 return (double)o;
957 }
958 if (o is bool)
959 {
960 if (!(bool)o)
961 {
962 return 0.0;
963 }
964 return 1.0;
965 }
966 try
967 {
969 }
970 catch (FormatException)
971 {
972 }
973 catch (OverflowException)
974 {
975 }
977 {
978 }
979 return double.NaN;
980 }
981
982 internal static string ToXPathString(object value)
983 {
984 if (value is string result)
985 {
986 return result;
987 }
988 if (value is double num)
989 {
990 return num.ToString("R", NumberFormatInfo.InvariantInfo);
991 }
992 if (value is bool)
993 {
994 if (!(bool)value)
995 {
996 return "false";
997 }
998 return "true";
999 }
1001 }
1002
1003 internal static double XPathRound(double value)
1004 {
1005 double num = Math.Round(value);
1006 if (value - num != 0.5)
1007 {
1008 return num;
1009 }
1010 return num + 1.0;
1011 }
1012
1013 public static TimeSpan ToTimeSpan(string s)
1014 {
1016 try
1017 {
1018 xsdDuration = new XsdDuration(s);
1019 }
1020 catch (Exception)
1021 {
1022 throw new FormatException(System.SR.Format(System.SR.XmlConvert_BadFormat, s, "TimeSpan"));
1023 }
1024 return xsdDuration.ToTimeSpan();
1025 }
1026
1027 internal static Exception TryToTimeSpan(string s, out TimeSpan result)
1028 {
1031 if (ex != null)
1032 {
1033 result = TimeSpan.MinValue;
1034 return ex;
1035 }
1036 return result2.TryToTimeSpan(out result);
1037 }
1038
1039 private static void CreateAllDateTimeFormats()
1040 {
1041 if (s_allDateTimeFormats == null)
1042 {
1043 s_allDateTimeFormats = new string[24]
1044 {
1045 "yyyy-MM-ddTHH:mm:ss.FFFFFFFzzzzzz", "yyyy-MM-ddTHH:mm:ss.FFFFFFF", "yyyy-MM-ddTHH:mm:ss.FFFFFFFZ", "HH:mm:ss.FFFFFFF", "HH:mm:ss.FFFFFFFZ", "HH:mm:ss.FFFFFFFzzzzzz", "yyyy-MM-dd", "yyyy-MM-ddZ", "yyyy-MM-ddzzzzzz", "yyyy-MM",
1046 "yyyy-MMZ", "yyyy-MMzzzzzz", "yyyy", "yyyyZ", "yyyyzzzzzz", "--MM-dd", "--MM-ddZ", "--MM-ddzzzzzz", "---dd", "---ddZ",
1047 "---ddzzzzzz", "--MM--", "--MM--Z", "--MM--zzzzzz"
1048 };
1049 }
1050 }
1051
1052 [Obsolete("Use XmlConvert.ToDateTime() that accepts an XmlDateTimeSerializationMode instead.")]
1053 public static DateTime ToDateTime(string s)
1054 {
1056 }
1057
1058 public static DateTime ToDateTime(string s, string format)
1059 {
1060 return DateTime.ParseExact(s, format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);
1061 }
1062
1063 public static DateTime ToDateTime(string s, string[] formats)
1064 {
1065 return DateTime.ParseExact(s, formats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);
1066 }
1067
1069 {
1072 switch (dateTimeOption)
1073 {
1076 break;
1079 break;
1080 case XmlDateTimeSerializationMode.Unspecified:
1081 dateTime = new DateTime(dateTime.Ticks, DateTimeKind.Unspecified);
1082 break;
1083 default:
1085 case XmlDateTimeSerializationMode.RoundtripKind:
1086 break;
1087 }
1088 return dateTime;
1089 }
1090
1091 public static DateTimeOffset ToDateTimeOffset(string s)
1092 {
1093 if (s == null)
1094 {
1095 throw new ArgumentNullException("s");
1096 }
1098 return xsdDateTime;
1099 }
1100
1101 public static DateTimeOffset ToDateTimeOffset(string s, string format)
1102 {
1103 if (s == null)
1104 {
1105 throw new ArgumentNullException("s");
1106 }
1107 return DateTimeOffset.ParseExact(s, format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);
1108 }
1109
1110 public static DateTimeOffset ToDateTimeOffset(string s, string[] formats)
1111 {
1112 if (s == null)
1113 {
1114 throw new ArgumentNullException("s");
1115 }
1116 return DateTimeOffset.ParseExact(s, formats, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AllowLeadingWhite | DateTimeStyles.AllowTrailingWhite);
1117 }
1118
1119 public static Guid ToGuid(string s)
1120 {
1121 return new Guid(s);
1122 }
1123
1124 internal static Exception TryToGuid(string s, out Guid result)
1125 {
1126 Exception result2 = null;
1127 result = Guid.Empty;
1128 try
1129 {
1130 result = new Guid(s);
1131 }
1132 catch (ArgumentException)
1133 {
1135 }
1136 catch (FormatException)
1137 {
1139 }
1140 return result2;
1141 }
1142
1144 {
1145 return value.Kind switch
1146 {
1149 DateTimeKind.Utc => value.ToLocalTime(),
1150 _ => value,
1151 };
1152 }
1153
1155 {
1156 return value.Kind switch
1157 {
1160 DateTimeKind.Local => value.ToUniversalTime(),
1161 _ => value,
1162 };
1163 }
1164
1165 internal static Uri ToUri(string s)
1166 {
1167 if (!string.IsNullOrEmpty(s))
1168 {
1169 s = TrimString(s);
1170 if (s.Length == 0 || s.IndexOf("##", StringComparison.Ordinal) != -1)
1171 {
1173 }
1174 }
1175 if (!Uri.TryCreate(s, UriKind.RelativeOrAbsolute, out Uri result))
1176 {
1178 }
1179 return result;
1180 }
1181
1182 internal static Exception TryToUri(string s, out Uri result)
1183 {
1184 result = null;
1185 if (s != null && s.Length > 0)
1186 {
1187 s = TrimString(s);
1188 if (s.Length == 0 || s.IndexOf("##", StringComparison.Ordinal) != -1)
1189 {
1191 }
1192 }
1193 if (!Uri.TryCreate(s, UriKind.RelativeOrAbsolute, out result))
1194 {
1196 }
1197 return null;
1198 }
1199
1200 internal static bool StrEqual(char[] chars, int strPos1, int strLen1, string str2)
1201 {
1202 if (strLen1 != str2.Length)
1203 {
1204 return false;
1205 }
1206 int i;
1207 for (i = 0; i < strLen1 && chars[strPos1 + i] == str2[i]; i++)
1208 {
1209 }
1210 return i == strLen1;
1211 }
1212
1213 internal static string TrimString(string value)
1214 {
1215 return value.Trim(WhitespaceChars);
1216 }
1217
1218 internal static string TrimStringStart(string value)
1219 {
1220 return value.TrimStart(WhitespaceChars);
1221 }
1222
1223 internal static string TrimStringEnd(string value)
1224 {
1225 return value.TrimEnd(WhitespaceChars);
1226 }
1227
1228 internal static string[] SplitString(string value)
1229 {
1230 return value.Split(WhitespaceChars, StringSplitOptions.RemoveEmptyEntries);
1231 }
1232
1233 internal static string[] SplitString(string value, StringSplitOptions splitStringOptions)
1234 {
1236 }
1237
1238 internal static bool IsNegativeZero(double value)
1239 {
1241 {
1242 return true;
1243 }
1244 return false;
1245 }
1246
1247 internal static void VerifyCharData(string data, ExceptionType exceptionType)
1248 {
1250 }
1251
1253 {
1254 if (data == null || data.Length == 0)
1255 {
1256 return;
1257 }
1258 int num = 0;
1259 int length = data.Length;
1260 while (true)
1261 {
1262 if (num < length && XmlCharType.IsCharData(data[num]))
1263 {
1264 num++;
1265 continue;
1266 }
1267 if (num == length)
1268 {
1269 return;
1270 }
1271 char ch = data[num];
1273 {
1274 break;
1275 }
1276 if (num + 1 == length)
1277 {
1279 }
1280 ch = data[num + 1];
1282 {
1283 num += 2;
1284 continue;
1285 }
1286 throw CreateInvalidSurrogatePairException(data[num + 1], data[num], invSurrogateExceptionType, 0, num + 1);
1287 }
1289 }
1290
1291 internal static void VerifyCharData(char[] data, int offset, int len, ExceptionType exceptionType)
1292 {
1293 if (data == null || len == 0)
1294 {
1295 return;
1296 }
1297 int num = offset;
1298 int num2 = offset + len;
1299 while (true)
1300 {
1301 if (num < num2 && XmlCharType.IsCharData(data[num]))
1302 {
1303 num++;
1304 continue;
1305 }
1306 if (num == num2)
1307 {
1308 return;
1309 }
1310 char ch = data[num];
1312 {
1313 break;
1314 }
1315 if (num + 1 == num2)
1316 {
1318 }
1319 ch = data[num + 1];
1321 {
1322 num += 2;
1323 continue;
1324 }
1325 throw CreateInvalidSurrogatePairException(data[num + 1], data[num], exceptionType, 0, offset - num + 1);
1326 }
1327 throw CreateInvalidCharException(data, len, num, exceptionType);
1328 }
1329
1330 internal static string EscapeValueForDebuggerDisplay(string value)
1331 {
1333 int i = 0;
1334 int num = 0;
1335 for (; i < value.Length; i++)
1336 {
1337 char c = value[i];
1338 if (c < ' ' || c == '"')
1339 {
1340 if (stringBuilder == null)
1341 {
1342 stringBuilder = new StringBuilder(value.Length + 4);
1343 }
1344 if (i - num > 0)
1345 {
1346 stringBuilder.Append(value, num, i - num);
1347 }
1348 num = i + 1;
1349 switch (c)
1350 {
1351 case '"':
1352 stringBuilder.Append("\\\"");
1353 break;
1354 case '\r':
1355 stringBuilder.Append("\\r");
1356 break;
1357 case '\n':
1358 stringBuilder.Append("\\n");
1359 break;
1360 case '\t':
1361 stringBuilder.Append("\\t");
1362 break;
1363 default:
1364 stringBuilder.Append(c);
1365 break;
1366 }
1367 }
1368 }
1369 if (stringBuilder == null)
1370 {
1371 return value;
1372 }
1373 if (i - num > 0)
1374 {
1375 stringBuilder.Append(value, num, i - num);
1376 }
1377 return stringBuilder.ToString();
1378 }
1379
1380 internal static Exception CreateException(string res, ExceptionType exceptionType, int lineNo, int linePos)
1381 {
1382 return exceptionType switch
1383 {
1385 _ => new XmlException(res, string.Empty, lineNo, linePos),
1386 };
1387 }
1388
1390 {
1391 return CreateException(res, arg, exceptionType, 0, 0);
1392 }
1393
1394 internal static Exception CreateException(string res, string arg, ExceptionType exceptionType, int lineNo, int linePos)
1395 {
1396 return exceptionType switch
1397 {
1399 _ => new XmlException(res, arg, lineNo, linePos),
1400 };
1401 }
1402
1404 {
1405 return CreateException(res, args, exceptionType, 0, 0);
1406 }
1407
1408 internal static Exception CreateException(string res, string[] args, ExceptionType exceptionType, int lineNo, int linePos)
1409 {
1410 switch (exceptionType)
1411 {
1412 case ExceptionType.ArgumentException:
1413 return new ArgumentException(string.Format(res, args));
1414 default:
1415 return new XmlException(res, args, lineNo, linePos);
1416 }
1417 }
1418
1420 {
1421 return CreateInvalidSurrogatePairException(low, hi, ExceptionType.ArgumentException);
1422 }
1423
1428
1429 internal static Exception CreateInvalidSurrogatePairException(char low, char hi, ExceptionType exceptionType, int lineNo, int linePos)
1430 {
1431 string[] array = new string[2];
1432 uint num = hi;
1433 array[0] = num.ToString("X", CultureInfo.InvariantCulture);
1434 num = low;
1435 array[1] = num.ToString("X", CultureInfo.InvariantCulture);
1436 string[] args = array;
1438 }
1439
1441 {
1442 return CreateInvalidHighSurrogateCharException(hi, ExceptionType.ArgumentException);
1443 }
1444
1449
1451 {
1453 uint num = hi;
1454 return CreateException(xml_InvalidSurrogateHighChar, num.ToString("X", CultureInfo.InvariantCulture), exceptionType, lineNo, linePos);
1455 }
1456
1461
1462 internal static Exception CreateInvalidCharException(string data, int invCharPos)
1463 {
1464 return CreateInvalidCharException(data, invCharPos, ExceptionType.ArgumentException);
1465 }
1466
1471
1473 {
1474 return CreateInvalidCharException(invChar, nextChar, ExceptionType.ArgumentException);
1475 }
1476
1481
1486
1488 {
1489 if (name != null)
1490 {
1492 }
1494 }
1495}
static unsafe long DoubleToInt64Bits(double value)
static ? string ToString(object? value)
Definition Convert.cs:2321
static double ToDouble(object? value)
Definition Convert.cs:1991
static CultureInfo InvariantCulture
static int FromChar(int c)
static decimal Round(decimal d)
Definition Math.cs:1096
static string Sch_NotTokenString
Definition SR.cs:394
static string Xml_EmptyName
Definition SR.cs:292
static string Xml_BadStartNameChar
Definition SR.cs:42
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string Xml_InvalidSurrogatePairWithArgs
Definition SR.cs:326
static string Xml_InvalidSurrogateHighChar
Definition SR.cs:322
static string Xml_InvalidNmToken
Definition SR.cs:138
static string Xml_InvalidSurrogateMissingLowChar
Definition SR.cs:324
static string Sch_InvalidDateTimeOption
Definition SR.cs:396
static string Xml_EmptyLocalName
Definition SR.cs:294
static string Xml_InvalidCharacter
Definition SR.cs:110
static string XmlConvert_BadFormat
Definition SR.cs:370
static string Xml_InvalidWhitespaceCharacter
Definition SR.cs:200
static string Xml_BadNameChar
Definition SR.cs:44
static string XmlConvert_NotOneCharString
Definition SR.cs:390
static string Sch_NotNormalizedString
Definition SR.cs:780
Definition SR.cs:7
static bool TryCreate([NotNullWhen(true)] string? uriString, UriKind uriKind, [NotNullWhen(true)] out Uri? result)
Definition Uri.cs:3793
override int Decode(char[] chars, int startPos, int len)
static void Encode(byte[] buffer, int index, int count, XmlWriter writer)
static int ParseNameNoNamespaces(string s, int offset)
static Exception GetInvalidNameException(string s, int offsetStartChar, int offsetBadChar)
static int ParseQName(string s, int offset, out int colonOffset)
static int ParseNmtokenNoNamespaces(string s, int offset)
static int ParseNCName(string s, int offset)
static bool IsNCNameCharXml4e(char ch)
static void SplitSurrogateChar(int combinedChar, out char lowChar, out char highChar)
static bool IsStartNCNameSingleChar(char ch)
static int IsOnlyWhitespaceWithPos(string str)
static bool IsNameCharXml4e(char ch)
static bool IsNCNameSingleChar(char ch)
static bool IsWhiteSpace(char ch)
static bool IsLowSurrogate(int ch)
static bool IsPubidChar(char ch)
static bool IsStartNCNameCharXml4e(char ch)
static int CombineSurrogateChar(int lowChar, int highChar)
static bool IsCharData(char ch)
static bool IsHighSurrogate(int ch)
static int IsPublicId(string str)
static Exception TryVerifyNormalizedString(string str)
static bool ToBoolean(string s)
static string TrimStringEnd(string value)
static bool IsWhitespaceChar(char ch)
static double ToXPathDouble(object o)
static Exception TryVerifyTOKEN(string token)
static string TrimStringStart(string value)
static DateTimeOffset ToDateTimeOffset(string s)
static string VerifyNCName(string name, ExceptionType exceptionType)
static Exception CreateInvalidNameCharException(string name, int index, ExceptionType exceptionType)
static uint ToUInt32(string s)
static bool IsNegativeZero(double value)
static string ToString(sbyte value)
static Exception TryToByte(string s, out byte result)
static char[] crt
Definition XmlConvert.cs:14
static string ToString(int value)
static string[] SplitString(string value)
static DateTime SwitchToUtcTime(DateTime value)
static ? string VerifyTOKEN(string? token)
static string ToString(uint value)
static Exception CreateException(string res, string[] args, ExceptionType exceptionType)
static Exception TryVerifyNMTOKEN(string name)
static string ToBinHexString(byte[] inArray)
static string ToString(long value)
static ? string EncodeName(string? name)
Definition XmlConvert.cs:37
static DateTime SwitchToLocalTime(DateTime value)
static void VerifyCharData(string data, ExceptionType exceptionType)
static ? string DecodeName(string? name)
Definition XmlConvert.cs:55
static string ToString(float value)
static int ToInt32(string s)
static string[] SplitString(string value, StringSplitOptions splitStringOptions)
static string ToString(ushort value)
static short ToInt16(string s)
static Exception TryToBoolean(string s, out bool result)
static DateTime ToDateTime(string s, XmlDateTimeSerializationMode dateTimeOption)
static string ToString(bool value)
static Exception CreateException(string res, string arg, ExceptionType exceptionType)
static string TrimString(string value)
static Exception CreateInvalidCharException(string data, int invCharPos)
static byte[] FromBinHexString(string s, bool allowOddCount)
static string ToString(double value)
static float ToSingle(string s)
static ArgumentException CreateInvalidNameArgumentException(string name, string argumentName)
static string[] AllDateTimeFormats
Definition XmlConvert.cs:25
static DateTime ToDateTime(string s, string[] formats)
static Exception TryToDouble(string s, out double result)
static TimeSpan ToTimeSpan(string s)
static string ToString(DateTime value, XmlDateTimeSerializationMode dateTimeOption)
static string EncodeName(string name, bool first, bool local)
static bool IsXmlChar(char ch)
static Exception CreateInvalidCharException(string data, int invCharPos, ExceptionType exceptionType)
static string VerifyName(string name)
static Exception TryToDecimal(string s, out decimal result)
static readonly CultureInfo s_invariantCultureInfo
Definition XmlConvert.cs:12
static Exception TryToSingle(string s, out float result)
static ? string EncodeLocalName(string? name)
Definition XmlConvert.cs:49
static char ToChar(string s)
static string ToXPathString(object value)
static readonly char[] WhitespaceChars
Definition XmlConvert.cs:22
static Exception TryToUInt64(string s, out ulong result)
static Exception CreateInvalidCharException(char invChar, char nextChar)
static int FromHex(char digit)
static string ToString(ulong value)
static string VerifyNCName(string name)
static double ToDouble(string s)
static string ToString(decimal value)
static Exception TryToInt32(string s, out int result)
static DateTime ToDateTime(string s, string format)
static Exception TryToSByte(string s, out sbyte result)
static volatile string[] s_allDateTimeFormats
Definition XmlConvert.cs:20
static Exception TryToTimeSpan(string s, out TimeSpan result)
static string ToString(TimeSpan value)
static Exception CreateInvalidCharException(char[] data, int length, int invCharPos, ExceptionType exceptionType)
static Exception CreateException(string res, string[] args, ExceptionType exceptionType, int lineNo, int linePos)
static string ToString(Guid value)
static Exception TryToInteger(string s, out decimal result)
static Exception TryToUInt32(string s, out uint result)
static sbyte ToSByte(string s)
static Exception TryToInt64(string s, out long result)
static void CreateAllDateTimeFormats()
static Exception TryToUri(string s, out Uri result)
static Exception CreateInvalidSurrogatePairException(char low, char hi, ExceptionType exceptionType, int lineNo, int linePos)
static Exception TryToInt16(string s, out short result)
static string ToString(DateTimeOffset value)
static string ToString(char value)
static string VerifyPublicId(string publicId)
static string VerifyWhitespace(string content)
static string ToString(byte value)
static bool IsXmlSurrogatePair(char lowChar, char highChar)
static Exception TryToUInt16(string s, out ushort result)
static Guid ToGuid(string s)
static Uri ToUri(string s)
static void VerifyCharData(string data, ExceptionType invCharExceptionType, ExceptionType invSurrogateExceptionType)
static Exception TryToChar(string s, out char result)
static Exception TryToGuid(string s, out Guid result)
static ? string EncodeNmToken(string? name)
Definition XmlConvert.cs:43
static volatile Regex s_decodeCharPattern
Definition XmlConvert.cs:18
static DateTimeOffset ToDateTimeOffset(string s, string[] formats)
static Exception CreateInvalidSurrogatePairException(char low, char hi)
static string ToString(DateTimeOffset value, string format)
static bool IsPublicIdChar(char ch)
static bool StrEqual(char[] chars, int strPos1, int strLen1, string str2)
static Exception CreateInvalidSurrogatePairException(char low, char hi, ExceptionType exceptionType)
static string ToString(short value)
static DateTime ToDateTime(string s)
static volatile Regex s_encodeCharPattern
Definition XmlConvert.cs:16
static void VerifyCharData(char[] data, int offset, int len, ExceptionType exceptionType)
static decimal ToDecimal(string s)
static Exception CreateInvalidHighSurrogateCharException(char hi, ExceptionType exceptionType)
static string ToString(DateTime value, string format)
static Exception CreateException(string res, string arg, ExceptionType exceptionType, int lineNo, int linePos)
static byte[] FromBinHexString(string s)
static Exception CreateInvalidHighSurrogateCharException(char hi, ExceptionType exceptionType, int lineNo, int linePos)
static ulong ToUInt64(string s)
static string EscapeValueForDebuggerDisplay(string value)
static ushort ToUInt16(string s)
static Exception TryVerifyName(string name)
static Exception CreateInvalidHighSurrogateCharException(char hi)
static bool IsStartNCNameChar(char ch)
static decimal ToInteger(string s)
static byte ToByte(string s)
static Exception CreateInvalidCharException(char invChar, char nextChar, ExceptionType exceptionType)
static string VerifyXmlChars(string content)
static string VerifyNMTOKEN(string name)
static string VerifyQName(string name, ExceptionType exceptionType)
static Exception CreateException(string res, ExceptionType exceptionType, int lineNo, int linePos)
static string VerifyNMTOKEN(string name, ExceptionType exceptionType)
static Exception TryVerifyNCName(string name)
static double XPathRound(double value)
static long ToInt64(string s)
static string ToString(DateTime value)
static DateTimeOffset ToDateTimeOffset(string s, string format)
static bool IsNCNameChar(char ch)
static string[] BuildCharExceptionArgs(string data, int invCharIndex)
UriKind
Definition UriKind.cs:4
static DateTimeOffset ParseExact(string input, string format, IFormatProvider? formatProvider)
static DateTime ParseExact(string s, string format, IFormatProvider? provider)
Definition DateTime.cs:953
static readonly Guid Empty
Definition Guid.cs:86
static readonly TimeSpan MinValue
Definition TimeSpan.cs:25
static Exception TryParse(string s, out XsdDuration result)