ConfigXmlDocument.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // System.Configuration.ConfigXmlDocument
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.IO;
  31. #if (XML_DEP)
  32. using System.Xml;
  33. namespace System.Configuration
  34. {
  35. public sealed class ConfigXmlDocument : XmlDocument, IConfigXmlNode
  36. {
  37. XmlTextReader reader;
  38. string fileName;
  39. int lineNumber;
  40. public override XmlAttribute CreateAttribute (string prefix,
  41. string localName,
  42. string namespaceUri)
  43. {
  44. return new ConfigXmlAttribute (this, prefix, localName, namespaceUri);
  45. }
  46. public override XmlCDataSection CreateCDataSection (string data)
  47. {
  48. return new ConfigXmlCDataSection (this, data);
  49. }
  50. public override XmlComment CreateComment (string comment)
  51. {
  52. return new ConfigXmlComment (this, comment);
  53. }
  54. public override XmlElement CreateElement (string prefix, string localName, string namespaceUri)
  55. {
  56. return new ConfigXmlElement (this, prefix, localName, namespaceUri);
  57. }
  58. public override XmlSignificantWhitespace CreateSignificantWhitespace (string data)
  59. {
  60. return base.CreateSignificantWhitespace (data);
  61. }
  62. public override XmlText CreateTextNode (string text)
  63. {
  64. return new ConfigXmlText (this, text);
  65. }
  66. public override XmlWhitespace CreateWhitespace (string data)
  67. {
  68. return base.CreateWhitespace (data);
  69. }
  70. public override void Load (string filename)
  71. {
  72. XmlTextReader rd = new XmlTextReader (filename);
  73. rd.MoveToContent ();
  74. LoadSingleElement (filename, rd);
  75. }
  76. public void LoadSingleElement (string filename, XmlTextReader sourceReader)
  77. {
  78. fileName = filename;
  79. lineNumber = sourceReader.LineNumber;
  80. string xml = sourceReader.ReadOuterXml();
  81. reader = new XmlTextReader (new StringReader (xml), sourceReader.NameTable);
  82. Load (reader);
  83. reader.Close ();
  84. }
  85. public string Filename
  86. {
  87. get {
  88. return fileName;
  89. }
  90. }
  91. public int LineNumber
  92. {
  93. get {
  94. return lineNumber;
  95. }
  96. }
  97. //
  98. // Wrappers for Xml* that just provide file name and line number addition
  99. //
  100. class ConfigXmlAttribute : XmlAttribute, IConfigXmlNode
  101. {
  102. string fileName;
  103. int lineNumber;
  104. public ConfigXmlAttribute (ConfigXmlDocument document,
  105. string prefix,
  106. string localName,
  107. string namespaceUri)
  108. : base (prefix, localName, namespaceUri, document)
  109. {
  110. fileName = document.Filename;
  111. lineNumber = document.LineNumber;
  112. }
  113. public string Filename
  114. {
  115. get {
  116. return fileName;
  117. }
  118. }
  119. public int LineNumber
  120. {
  121. get {
  122. return lineNumber;
  123. }
  124. }
  125. }
  126. class ConfigXmlCDataSection : XmlCDataSection, IConfigXmlNode
  127. {
  128. string fileName;
  129. int lineNumber;
  130. public ConfigXmlCDataSection (ConfigXmlDocument document, string data)
  131. : base (data, document)
  132. {
  133. fileName = document.Filename;
  134. lineNumber = document.LineNumber;
  135. }
  136. public string Filename
  137. {
  138. get {
  139. return fileName;
  140. }
  141. }
  142. public int LineNumber
  143. {
  144. get {
  145. return lineNumber;
  146. }
  147. }
  148. }
  149. class ConfigXmlComment : XmlComment, IConfigXmlNode
  150. {
  151. string fileName;
  152. int lineNumber;
  153. public ConfigXmlComment (ConfigXmlDocument document, string comment)
  154. : base (comment, document)
  155. {
  156. fileName = document.Filename;
  157. lineNumber = document.LineNumber;
  158. }
  159. public string Filename
  160. {
  161. get {
  162. return fileName;
  163. }
  164. }
  165. public int LineNumber
  166. {
  167. get {
  168. return lineNumber;
  169. }
  170. }
  171. }
  172. class ConfigXmlElement : XmlElement, IConfigXmlNode
  173. {
  174. string fileName;
  175. int lineNumber;
  176. public ConfigXmlElement (ConfigXmlDocument document,
  177. string prefix,
  178. string localName,
  179. string namespaceUri)
  180. : base (prefix, localName, namespaceUri, document)
  181. {
  182. fileName = document.Filename;
  183. lineNumber = document.LineNumber;
  184. }
  185. public string Filename
  186. {
  187. get {
  188. return fileName;
  189. }
  190. }
  191. public int LineNumber
  192. {
  193. get {
  194. return lineNumber;
  195. }
  196. }
  197. }
  198. class ConfigXmlText : XmlText, IConfigXmlNode
  199. {
  200. string fileName;
  201. int lineNumber;
  202. public ConfigXmlText (ConfigXmlDocument document, string data)
  203. : base (data, document)
  204. {
  205. fileName = document.Filename;
  206. lineNumber = document.LineNumber;
  207. }
  208. public string Filename
  209. {
  210. get {
  211. return fileName;
  212. }
  213. }
  214. public int LineNumber
  215. {
  216. get {
  217. return lineNumber;
  218. }
  219. }
  220. }
  221. }
  222. }
  223. #endif