The Labs \ Source Viewer \ SSCLI \ System.Xml.XPath \ XPathException

  1. //------------------------------------------------------------------------------
  2. // <copyright file="XPathException.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. namespace System.Xml.XPath
  16. {
  17.     using System;
  18.     using System.IO;
  19.     using System.Resources;
  20.     using System.Runtime.Serialization;
  21.     using System.Diagnostics;
  22.     using System.Security.Permissions;
  23.    
  24.     // Represents the exception that is thrown when there is error processing an
  25.     // XPath expression.
  26.     [Serializable()]
  27.     public class XPathException : SystemException
  28.     {
  29.         // we need to keep this members for V1 serialization compatibility
  30.         string res;
  31.         string[] args;
  32.        
  33.         string message;
  34.        
  35.         protected XPathException(SerializationInfo info, StreamingContext context) : base(info, context)
  36.         {
  37.             res = (string)info.GetValue("res", typeof(string));
  38.             args = (string[])info.GetValue("args", typeof(string[]));
  39.            
  40.             // deserialize optional members
  41.             string version = null;
  42.             foreach (SerializationEntry e in info) {
  43.                 if (e.Name == "version") {
  44.                     version = (string)e.Value;
  45.                 }
  46.             }
  47.            
  48.             if (version == null) {
  49.                 // deserializing V1 exception
  50.                 message = CreateMessage(res, args);
  51.             }
  52.             else {
  53.                 // deserializing V2 or higher exception -> exception message is serialized by the base class (Exception._message)
  54.                 message = null;
  55.             }
  56.         }
  57.        
  58.         [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter = true)]
  59.         public override void GetObjectData(SerializationInfo info, StreamingContext context)
  60.         {
  61.             base.GetObjectData(info, context);
  62.             info.AddValue("res", res);
  63.             info.AddValue("args", args);
  64.             info.AddValue("version", "2.0");
  65.         }
  66.        
  67.         public XPathException() : this(string.Empty, (Exception)null)
  68.         {
  69.         }
  70.        
  71.         public XPathException(string message) : this(message, (Exception)null)
  72.         {
  73.         }
  74.        
  75.         public XPathException(string message, Exception innerException) : this(Res.Xml_UserException, new string[] {message}, innerException)
  76.         {
  77.         }
  78.        
  79.         static internal XPathException Create(string res)
  80.         {
  81.             return new XPathException(res, (string[])null);
  82.         }
  83.        
  84.         static internal XPathException Create(string res, string arg)
  85.         {
  86.             return new XPathException(res, new string[] {arg});
  87.         }
  88.        
  89.         static internal XPathException Create(string res, string arg, string arg2)
  90.         {
  91.             return new XPathException(res, new string[] {arg, arg2});
  92.         }
  93.        
  94.         static internal XPathException Create(string res, string arg, Exception innerException)
  95.         {
  96.             return new XPathException(res, new string[] {arg}, innerException);
  97.         }
  98.        
  99.         private XPathException(string res, string[] args) : this(res, args, null)
  100.         {
  101.         }
  102.        
  103.         private XPathException(string res, string[] args, Exception inner) : base(CreateMessage(res, args), inner)
  104.         {
  105.             HResult = HResults.XmlXPath;
  106.             this.res = res;
  107.             this.args = args;
  108.         }
  109.        
  110.         private static string CreateMessage(string res, string[] args)
  111.         {
  112.             try {
  113.                 string message = Res.GetString(res, args);
  114.                 if (message == null)
  115.                     message = "UNKNOWN(" + res + ")";
  116.                 return message;
  117.             }
  118.             catch (MissingManifestResourceException) {
  119.                 return "UNKNOWN(" + res + ")";
  120.             }
  121.         }
  122.        
  123.         public override string Message {
  124.             get { return (message == null) ? base.Message : message; }
  125.         }
  126.     }
  127. }

Developer Fusion