XmlException.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // XmlException.cs
  3. //
  4. // Author:
  5. // Jason Diamond ([email protected])
  6. // Atsushi Enomoto ([email protected])
  7. //
  8. // (C) 2002 Jason Diamond http://injektilo.org/
  9. // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Globalization;
  31. using System.Runtime.Serialization;
  32. using System.Security.Permissions;
  33. namespace System.Xml
  34. {
  35. [Serializable]
  36. public class XmlException : SystemException
  37. {
  38. #region Fields
  39. int lineNumber;
  40. int linePosition;
  41. string sourceUri;
  42. string res;
  43. string [] messages;
  44. #endregion
  45. #region consts
  46. private const string Xml_DefaultException = "Xml_DefaultException";
  47. private const string Xml_UserException = "Xml_UserException";
  48. #endregion
  49. #region Constructors
  50. public XmlException ()
  51. : base ()
  52. {
  53. this.res = Xml_DefaultException;
  54. this.messages = new string [1];
  55. }
  56. public XmlException (string message, Exception innerException)
  57. : base (message, innerException)
  58. {
  59. this.res = Xml_UserException;
  60. this.messages = new string [] {message};
  61. }
  62. protected XmlException (SerializationInfo info, StreamingContext context)
  63. : base (info, context)
  64. {
  65. this.lineNumber = info.GetInt32 ("lineNumber");
  66. this.linePosition = info.GetInt32 ("linePosition");
  67. this.res = info.GetString ("res");
  68. this.messages = (string []) info.GetValue ("args", typeof(string []));
  69. #if NET_2_0
  70. this.sourceUri = info.GetString ("sourceUri");
  71. #endif
  72. }
  73. public XmlException (string message)
  74. : base (message)
  75. {
  76. this.res = Xml_UserException;
  77. this.messages = new string [] {message};
  78. }
  79. internal XmlException (IXmlLineInfo li,
  80. string sourceUri,
  81. string message)
  82. : this (li, null, sourceUri, message)
  83. {
  84. }
  85. internal XmlException (IXmlLineInfo li,
  86. Exception innerException,
  87. string sourceUri,
  88. string message)
  89. : this (message, innerException)
  90. {
  91. if (li != null) {
  92. this.lineNumber = li.LineNumber;
  93. this.linePosition = li.LinePosition;
  94. }
  95. this.sourceUri = sourceUri;
  96. }
  97. public XmlException (string message, Exception innerException, int lineNumber, int linePosition)
  98. : this (message, innerException)
  99. {
  100. this.lineNumber = lineNumber;
  101. this.linePosition = linePosition;
  102. }
  103. #endregion
  104. #region Properties
  105. public int LineNumber {
  106. get { return lineNumber; }
  107. }
  108. public int LinePosition {
  109. get { return linePosition; }
  110. }
  111. #if NET_2_0
  112. public string SourceUri {
  113. get { return sourceUri; }
  114. }
  115. #endif
  116. public override string Message {
  117. get {
  118. if (lineNumber == 0)
  119. return base.Message;
  120. return String.Format (CultureInfo.InvariantCulture, "{0} {3} Line {1}, position {2}.",
  121. base.Message, lineNumber, linePosition, sourceUri);
  122. }
  123. }
  124. #endregion
  125. #region Methods
  126. [SecurityPermission (SecurityAction.Demand, SerializationFormatter = true)]
  127. public override void GetObjectData (SerializationInfo info, StreamingContext context)
  128. {
  129. base.GetObjectData (info, context);
  130. info.AddValue ("lineNumber", lineNumber);
  131. info.AddValue ("linePosition", linePosition);
  132. info.AddValue ("res", res);
  133. info.AddValue ("args", messages);
  134. #if NET_2_0
  135. info.AddValue ("sourceUri", sourceUri);
  136. #endif
  137. }
  138. #endregion
  139. }
  140. }