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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="ConfigXmlDocument.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.     // ConfigXmlDocument - 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.     [PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
  30.     public sealed class ConfigXmlDocument : XmlDocument, IConfigErrorInfo
  31.     {
  32.         XmlTextReader _reader;
  33.         int _lineOffset;
  34.         string _filename;
  35.        
  36.         int IConfigErrorInfo.LineNumber {
  37.             get {
  38.                 if (_reader == null) {
  39.                     return 0;
  40.                 }
  41.                
  42.                 if (_lineOffset > 0) {
  43.                     return _reader.LineNumber + _lineOffset - 1;
  44.                 }
  45.                
  46.                 return _reader.LineNumber;
  47.             }
  48.         }
  49.        
  50.         public int LineNumber {
  51.             get { return ((IConfigErrorInfo)this).LineNumber; }
  52.         }
  53.        
  54.         public string Filename {
  55.             get { return ConfigurationException.SafeFilename(_filename); }
  56.         }
  57.        
  58.         string IConfigErrorInfo.Filename {
  59.             get { return _filename; }
  60.         }
  61.        
  62.         public override void Load(string filename)
  63.         {
  64.             _filename = filename;
  65.             try {
  66.                 _reader = new XmlTextReader(filename);
  67.                 _reader.XmlResolver = null;
  68.                 base.Load(_reader);
  69.             }
  70.             finally {
  71.                 if (_reader != null) {
  72.                     _reader.Close();
  73.                     _reader = null;
  74.                 }
  75.             }
  76.         }
  77.        
  78.        
  79.         public void LoadSingleElement(string filename, XmlTextReader sourceReader)
  80.         {
  81.             _filename = filename;
  82.             _lineOffset = sourceReader.LineNumber;
  83.             string outerXml = sourceReader.ReadOuterXml();
  84.            
  85.             try {
  86.                 _reader = new XmlTextReader(new StringReader(outerXml), sourceReader.NameTable);
  87.                 base.Load(_reader);
  88.             }
  89.             finally {
  90.                 if (_reader != null) {
  91.                     _reader.Close();
  92.                     _reader = null;
  93.                 }
  94.             }
  95.         }
  96.        
  97.         public override XmlAttribute CreateAttribute(string prefix, string localName, string namespaceUri)
  98.         {
  99.             return new ConfigXmlAttribute(_filename, LineNumber, prefix, localName, namespaceUri, this);
  100.         }
  101.         public override XmlElement CreateElement(string prefix, string localName, string namespaceUri)
  102.         {
  103.             return new ConfigXmlElement(_filename, LineNumber, prefix, localName, namespaceUri, this);
  104.         }
  105.         public override XmlText CreateTextNode(string text)
  106.         {
  107.             return new ConfigXmlText(_filename, LineNumber, text, this);
  108.         }
  109.         public override XmlCDataSection CreateCDataSection(string data)
  110.         {
  111.             return new ConfigXmlCDataSection(_filename, LineNumber, data, this);
  112.         }
  113.         public override XmlComment CreateComment(string data)
  114.         {
  115.             return new ConfigXmlComment(_filename, LineNumber, data, this);
  116.         }
  117.         public override XmlSignificantWhitespace CreateSignificantWhitespace(string data)
  118.         {
  119.             return new ConfigXmlSignificantWhitespace(_filename, LineNumber, data, this);
  120.         }
  121.         public override XmlWhitespace CreateWhitespace(string data)
  122.         {
  123.             return new ConfigXmlWhitespace(_filename, LineNumber, data, this);
  124.         }
  125.     }
  126. }

Developer Fusion