The Labs \ Source Viewer \ SSCLI \ System.Configuration \ ErrorInfoXmlDocument

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ErrorInfoXmlDocument.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.Configuration
  16. {
  17.     using System.Configuration.Internal;
  18.     using System.IO;
  19.     using System.Xml;
  20.     using System.Security.Permissions;
  21.    
  22.     // ErrorInfoXmlDocument - the default Xml Document doesn't track line numbers, and line
  23.     // numbers are necessary to display source on config errors.
  24.     // These classes wrap corresponding System.Xml types and also carry
  25.     // the necessary information for reporting filename / line numbers.
  26.     // Note: these classes will go away if webdata ever decides to incorporate line numbers
  27.     // into the default XML classes. This class could also go away if webdata brings back
  28.     // the UserData property to hang any info off of any node.
  29.     internal sealed class ErrorInfoXmlDocument : XmlDocument, IConfigErrorInfo
  30.     {
  31.         XmlTextReader _reader;
  32.         int _lineOffset;
  33.         string _filename;
  34.        
  35.         int IConfigErrorInfo.LineNumber {
  36.             get {
  37.                 if (_reader == null) {
  38.                     return 0;
  39.                 }
  40.                
  41.                 if (_lineOffset > 0) {
  42.                     return _reader.LineNumber + _lineOffset - 1;
  43.                 }
  44.                
  45.                 return _reader.LineNumber;
  46.             }
  47.         }
  48.        
  49.         internal int LineNumber {
  50.             get { return ((IConfigErrorInfo)this).LineNumber; }
  51.         }
  52.        
  53.         string IConfigErrorInfo.Filename {
  54.             get { return _filename; }
  55.         }
  56.        
  57.         public override void Load(string filename)
  58.         {
  59.             _filename = filename;
  60.             try {
  61.                 _reader = new XmlTextReader(filename);
  62.                 _reader.XmlResolver = null;
  63.                 base.Load(_reader);
  64.             }
  65.             finally {
  66.                 if (_reader != null) {
  67.                     _reader.Close();
  68.                     _reader = null;
  69.                 }
  70.             }
  71.         }
  72.        
  73.         private void LoadFromConfigXmlReader(ConfigXmlReader reader)
  74.         {
  75.             IConfigErrorInfo err = (IConfigErrorInfo)reader;
  76.             _filename = err.Filename;
  77.             _lineOffset = err.LineNumber + 1;
  78.            
  79.             try {
  80.                 _reader = reader;
  81.                 base.Load(_reader);
  82.             }
  83.             finally {
  84.                 if (_reader != null) {
  85.                     _reader.Close();
  86.                     _reader = null;
  87.                 }
  88.             }
  89.         }
  90.        
  91.         static internal XmlNode CreateSectionXmlNode(ConfigXmlReader reader)
  92.         {
  93.             ErrorInfoXmlDocument doc = new ErrorInfoXmlDocument();
  94.             doc.LoadFromConfigXmlReader(reader);
  95.             XmlNode xmlNode = doc.DocumentElement;
  96.            
  97.             return xmlNode;
  98.         }
  99.        
  100.        
  101.         public override XmlAttribute CreateAttribute(string prefix, string localName, string namespaceUri)
  102.         {
  103.             return new ConfigXmlAttribute(_filename, LineNumber, prefix, localName, namespaceUri, this);
  104.         }
  105.         public override XmlElement CreateElement(string prefix, string localName, string namespaceUri)
  106.         {
  107.             return new ConfigXmlElement(_filename, LineNumber, prefix, localName, namespaceUri, this);
  108.         }
  109.         public override XmlText CreateTextNode(string text)
  110.         {
  111.             return new ConfigXmlText(_filename, LineNumber, text, this);
  112.         }
  113.         public override XmlCDataSection CreateCDataSection(string data)
  114.         {
  115.             return new ConfigXmlCDataSection(_filename, LineNumber, data, this);
  116.         }
  117.         public override XmlComment CreateComment(string data)
  118.         {
  119.             return new ConfigXmlComment(_filename, LineNumber, data, this);
  120.         }
  121.         public override XmlSignificantWhitespace CreateSignificantWhitespace(string data)
  122.         {
  123.             return new ConfigXmlSignificantWhitespace(_filename, LineNumber, data, this);
  124.         }
  125.         public override XmlWhitespace CreateWhitespace(string data)
  126.         {
  127.             return new ConfigXmlWhitespace(_filename, LineNumber, data, this);
  128.         }
  129.     }
  130. }

Developer Fusion