ConfigurationSettings.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  1. //
  2. // System.Configuration.ConfigurationSettings.cs
  3. //
  4. // Author:
  5. // Christopher Podurgiel ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. // Eric Lindvall ([email protected])
  8. //
  9. // (c) Christopher Podurgiel
  10. // (c) 2002 Ximian, Inc. (http://www.ximian.com)
  11. // (c) 2003 Novell, Inc. (http://www.novell.com)
  12. //
  13. //
  14. // Permission is hereby granted, free of charge, to any person obtaining
  15. // a copy of this software and associated documentation files (the
  16. // "Software"), to deal in the Software without restriction, including
  17. // without limitation the rights to use, copy, modify, merge, publish,
  18. // distribute, sublicense, and/or sell copies of the Software, and to
  19. // permit persons to whom the Software is furnished to do so, subject to
  20. // the following conditions:
  21. //
  22. // The above copyright notice and this permission notice shall be
  23. // included in all copies or substantial portions of the Software.
  24. //
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  26. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  27. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  28. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  29. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  30. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  31. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  32. //
  33. #if CONFIGURATION_DEP && !TARGET_JVM
  34. extern alias PrebuiltSystem;
  35. using NameValueCollection = PrebuiltSystem.System.Collections.Specialized.NameValueCollection;
  36. #endif
  37. using System;
  38. using System.Collections;
  39. using System.Collections.Specialized;
  40. using System.IO;
  41. using System.Runtime.CompilerServices;
  42. using System.Security.Permissions;
  43. #if (XML_DEP)
  44. using System.Xml;
  45. using System.Xml.XPath;
  46. #endif
  47. #if TARGET_JVM
  48. using vmw.common;
  49. using [email protected];
  50. #endif
  51. namespace System.Configuration
  52. {
  53. public sealed class ConfigurationSettings
  54. {
  55. #if !TARGET_JVM
  56. static IConfigurationSystem config = DefaultConfig.GetInstance ();
  57. #else
  58. static IConfigurationSystem config {
  59. get {
  60. IConfigurationSystem conf = (IConfigurationSystem) AppDomain.CurrentDomain.GetData ("ConfigurationSettings.Config");
  61. if (conf == null) {
  62. conf = DefaultConfig.GetInstance ();
  63. AppDomain.CurrentDomain.SetData ("ConfigurationSettings.Config", conf);
  64. }
  65. return conf;
  66. }
  67. set {
  68. AppDomain.CurrentDomain.SetData ("ConfigurationSettings.Config", value);
  69. }
  70. }
  71. #endif
  72. static object lockobj = new object ();
  73. private ConfigurationSettings ()
  74. {
  75. }
  76. [Obsolete ("This method is obsolete, it has been replaced by System.Configuration!System.Configuration.ConfigurationManager.GetSection")]
  77. public static object GetConfig (string sectionName)
  78. {
  79. #if CONFIGURATION_DEP
  80. return ConfigurationManager.GetSection (sectionName);
  81. #else
  82. return config.GetConfig (sectionName);
  83. #endif
  84. }
  85. [Obsolete ("This property is obsolete. Please use System.Configuration.ConfigurationManager.AppSettings")]
  86. public static NameValueCollection AppSettings
  87. {
  88. get {
  89. #if CONFIGURATION_DEP
  90. object appSettings = ConfigurationManager.GetSection ("appSettings");
  91. #else
  92. object appSettings = GetConfig ("appSettings");
  93. #endif
  94. if (appSettings == null)
  95. appSettings = new NameValueCollection ();
  96. return (NameValueCollection) appSettings;
  97. }
  98. }
  99. // Invoked from System.Web, disable warning
  100. internal static IConfigurationSystem ChangeConfigurationSystem (IConfigurationSystem newSystem)
  101. {
  102. if (newSystem == null)
  103. throw new ArgumentNullException ("newSystem");
  104. lock (lockobj) {
  105. IConfigurationSystem old = config;
  106. config = newSystem;
  107. return old;
  108. }
  109. }
  110. }
  111. //
  112. // class DefaultConfig: read configuration from machine.config file and application
  113. // config file if available.
  114. //
  115. class DefaultConfig : IConfigurationSystem
  116. {
  117. #if !TARGET_JVM
  118. static readonly DefaultConfig instance = new DefaultConfig ();
  119. #else
  120. static DefaultConfig instance {
  121. get {
  122. DefaultConfig conf = (DefaultConfig) AppDomain.CurrentDomain.GetData ("DefaultConfig.instance");
  123. if (conf == null) {
  124. conf = new DefaultConfig ();
  125. AppDomain.CurrentDomain.SetData ("DefaultConfig.instance", conf);
  126. }
  127. return conf;
  128. }
  129. set {
  130. AppDomain.CurrentDomain.SetData ("DefaultConfig.instance", value);
  131. }
  132. }
  133. #endif
  134. ConfigurationData config;
  135. private DefaultConfig ()
  136. {
  137. }
  138. public static DefaultConfig GetInstance ()
  139. {
  140. return instance;
  141. }
  142. [Obsolete ("This method is obsolete. Please use System.Configuration.ConfigurationManager.GetConfig")]
  143. public object GetConfig (string sectionName)
  144. {
  145. Init ();
  146. return config.GetConfig (sectionName);
  147. }
  148. public void Init ()
  149. {
  150. lock (this) {
  151. if (config != null)
  152. return;
  153. ConfigurationData data = new ConfigurationData ();
  154. if (data.LoadString (GetBundledMachineConfig ())) {
  155. // do nothing
  156. } else {
  157. if (!data.Load (GetMachineConfigPath ()))
  158. throw new ConfigurationException ("Cannot find " + GetMachineConfigPath ());
  159. }
  160. string appfile = GetAppConfigPath ();
  161. if (appfile == null) {
  162. config = data;
  163. return;
  164. }
  165. ConfigurationData appData = new ConfigurationData (data);
  166. if (appData.Load (appfile))
  167. config = appData;
  168. else
  169. config = data;
  170. }
  171. }
  172. #if TARGET_JVM
  173. internal static string GetBundledMachineConfig ()
  174. {
  175. return null;
  176. }
  177. internal static string GetMachineConfigPath ()
  178. {
  179. return System.Runtime.InteropServices.RuntimeEnvironment.SystemConfigurationFile;
  180. }
  181. #else
  182. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  183. extern private static string get_bundled_machine_config ();
  184. internal static string GetBundledMachineConfig ()
  185. {
  186. return get_bundled_machine_config ();
  187. }
  188. [MethodImplAttribute(MethodImplOptions.InternalCall)]
  189. extern private static string get_machine_config_path ();
  190. internal static string GetMachineConfigPath ()
  191. {
  192. return get_machine_config_path ();
  193. }
  194. #endif
  195. private static string GetAppConfigPath ()
  196. {
  197. AppDomainSetup currentInfo = AppDomain.CurrentDomain.SetupInformation;
  198. string configFile = currentInfo.ConfigurationFile;
  199. if (configFile == null || configFile.Length == 0)
  200. return null;
  201. return configFile;
  202. }
  203. }
  204. enum AllowDefinition
  205. {
  206. Everywhere,
  207. MachineOnly,
  208. MachineToApplication
  209. }
  210. class SectionData
  211. {
  212. public readonly string SectionName;
  213. public readonly string TypeName;
  214. public readonly bool AllowLocation;
  215. public readonly AllowDefinition AllowDefinition;
  216. #if XML_DEP
  217. public string FileName;
  218. #endif
  219. public readonly bool RequirePermission;
  220. public SectionData (string sectionName, string typeName,
  221. bool allowLocation, AllowDefinition allowDefinition, bool requirePermission)
  222. {
  223. SectionName = sectionName;
  224. TypeName = typeName;
  225. AllowLocation = allowLocation;
  226. AllowDefinition = allowDefinition;
  227. RequirePermission = requirePermission;
  228. }
  229. }
  230. class ConfigurationData
  231. {
  232. ConfigurationData parent;
  233. Hashtable factories;
  234. static object removedMark = new object ();
  235. static object emptyMark = new object ();
  236. #if (XML_DEP)
  237. Hashtable pending;
  238. string fileName;
  239. static object groupMark = new object ();
  240. #endif
  241. Hashtable cache;
  242. Hashtable FileCache {
  243. get {
  244. if (cache != null)
  245. return cache;
  246. cache = new Hashtable ();
  247. return cache;
  248. }
  249. }
  250. public ConfigurationData () : this (null)
  251. {
  252. }
  253. public ConfigurationData (ConfigurationData parent)
  254. {
  255. this.parent = (parent == this) ? null : parent;
  256. factories = new Hashtable ();
  257. }
  258. // SECURITY-FIXME: limit this with an imperative assert for reading the specific file
  259. [FileIOPermission (SecurityAction.Assert, Unrestricted = true)]
  260. public bool Load (string fileName)
  261. {
  262. #if (XML_DEP)
  263. this.fileName = fileName;
  264. if (fileName == null
  265. #if !TARGET_JVM
  266. || !File.Exists (fileName)
  267. #endif
  268. )
  269. return false;
  270. XmlTextReader reader = null;
  271. try {
  272. #if !TARGET_JVM
  273. FileStream fs = new FileStream (fileName, FileMode.Open, FileAccess.Read);
  274. #else
  275. Stream fs = (Stream) vmw.common.IOUtils.getStream (fileName);
  276. //patch for machine.config
  277. if (fs == null && fileName.EndsWith ("machine.config")) {
  278. fs = (Stream) IOUtils.getStreamForGHConfigs (fileName);
  279. }
  280. if (fs == null) {
  281. return false;
  282. }
  283. #endif
  284. reader = new XmlTextReader (fs);
  285. if (InitRead (reader))
  286. ReadConfigFile (reader);
  287. } catch (ConfigurationException) {
  288. throw;
  289. } catch (Exception e) {
  290. throw new ConfigurationException ("Error reading " + fileName, e);
  291. } finally {
  292. if (reader != null)
  293. reader.Close();
  294. }
  295. #endif
  296. return true;
  297. }
  298. public bool LoadString (string data)
  299. {
  300. if (data == null)
  301. return false;
  302. #if (XML_DEP)
  303. XmlTextReader reader = null;
  304. try {
  305. TextReader tr = new StringReader (data);
  306. reader = new XmlTextReader (tr);
  307. if (InitRead (reader))
  308. ReadConfigFile (reader);
  309. } catch (ConfigurationException) {
  310. throw;
  311. } catch (Exception e) {
  312. throw new ConfigurationException ("Error reading " + fileName, e);
  313. } finally {
  314. if (reader != null)
  315. reader.Close();
  316. }
  317. #endif
  318. return true;
  319. }
  320. object GetHandler (string sectionName)
  321. {
  322. lock (factories) {
  323. object o = factories [sectionName];
  324. if (o == null || o == removedMark) {
  325. if (parent != null)
  326. return parent.GetHandler (sectionName);
  327. return null;
  328. }
  329. if (o is IConfigurationSectionHandler)
  330. return (IConfigurationSectionHandler) o;
  331. o = CreateNewHandler (sectionName, (SectionData) o);
  332. factories [sectionName] = o;
  333. return o;
  334. }
  335. }
  336. object CreateNewHandler (string sectionName, SectionData section)
  337. {
  338. Type t = Type.GetType (section.TypeName);
  339. if (t == null)
  340. throw new ConfigurationException ("Cannot get Type for " + section.TypeName);
  341. #if false
  342. Type iconfig = typeof (IConfigurationSectionHandler);
  343. if (!iconfig.IsAssignableFrom (t))
  344. throw new ConfigurationException (sectionName + " does not implement " + iconfig);
  345. #endif
  346. object o = Activator.CreateInstance (t, true);
  347. if (o == null)
  348. throw new ConfigurationException ("Cannot get instance for " + t);
  349. return o;
  350. }
  351. #if (XML_DEP)
  352. XmlDocument GetInnerDoc (XmlDocument doc, int i, string [] sectionPath)
  353. {
  354. if (++i >= sectionPath.Length)
  355. return doc;
  356. if (doc.DocumentElement == null)
  357. return null;
  358. XmlNode node = doc.DocumentElement.FirstChild;
  359. while (node != null) {
  360. if (node.Name == sectionPath [i]) {
  361. ConfigXmlDocument result = new ConfigXmlDocument ();
  362. result.Load (new StringReader (node.OuterXml));
  363. return GetInnerDoc (result, i, sectionPath);
  364. }
  365. node = node.NextSibling;
  366. }
  367. return null;
  368. }
  369. XmlDocument GetDocumentForSection (string sectionName)
  370. {
  371. ConfigXmlDocument doc = new ConfigXmlDocument ();
  372. if (pending == null)
  373. return doc;
  374. string [] sectionPath = sectionName.Split ('/');
  375. string outerxml = pending [sectionPath [0]] as string;
  376. if (outerxml == null)
  377. return doc;
  378. StringReader reader = new StringReader (outerxml);
  379. XmlTextReader rd = new XmlTextReader (reader);
  380. rd.MoveToContent ();
  381. doc.LoadSingleElement (fileName, rd);
  382. return GetInnerDoc (doc, 0, sectionPath);
  383. }
  384. object GetConfigInternal (string sectionName)
  385. {
  386. object handler = GetHandler (sectionName);
  387. IConfigurationSectionHandler iconf = handler as IConfigurationSectionHandler;
  388. if (iconf == null)
  389. return handler;
  390. object parentConfig = null;
  391. if (parent != null)
  392. parentConfig = parent.GetConfig (sectionName);
  393. XmlDocument doc = GetDocumentForSection (sectionName);
  394. if (doc == null || doc.DocumentElement == null)
  395. return parentConfig;
  396. return iconf.Create (parentConfig, fileName, doc.DocumentElement);
  397. }
  398. #else
  399. object GetConfigInternal (string sectionName)
  400. {
  401. return null;
  402. }
  403. #endif
  404. public object GetConfig (string sectionName)
  405. {
  406. object config;
  407. lock (this) {
  408. config = this.FileCache [sectionName];
  409. }
  410. if (config == emptyMark)
  411. return null;
  412. if (config != null)
  413. return config;
  414. lock (this) {
  415. config = GetConfigInternal (sectionName);
  416. this.FileCache [sectionName] = (config == null) ? emptyMark : config;
  417. }
  418. return config;
  419. }
  420. private object LookForFactory (string key)
  421. {
  422. object o = factories [key];
  423. if (o != null)
  424. return o;
  425. if (parent != null)
  426. return parent.LookForFactory (key);
  427. return null;
  428. }
  429. #if (XML_DEP)
  430. private bool InitRead (XmlTextReader reader)
  431. {
  432. reader.MoveToContent ();
  433. if (reader.NodeType != XmlNodeType.Element || reader.Name != "configuration")
  434. ThrowException ("Configuration file does not have a valid root element", reader);
  435. if (reader.HasAttributes)
  436. ThrowException ("Unrecognized attribute in root element", reader);
  437. if (reader.IsEmptyElement) {
  438. reader.Skip ();
  439. return false;
  440. }
  441. reader.Read ();
  442. reader.MoveToContent ();
  443. return reader.NodeType != XmlNodeType.EndElement;
  444. }
  445. // FIXME: this approach is not always safe and likely to cause bugs.
  446. private void MoveToNextElement (XmlTextReader reader)
  447. {
  448. while (reader.Read ()) {
  449. XmlNodeType ntype = reader.NodeType;
  450. if (ntype == XmlNodeType.Element)
  451. return;
  452. if (ntype != XmlNodeType.Whitespace &&
  453. ntype != XmlNodeType.Comment &&
  454. ntype != XmlNodeType.SignificantWhitespace &&
  455. ntype != XmlNodeType.EndElement)
  456. ThrowException ("Unrecognized element", reader);
  457. }
  458. }
  459. private void ReadSection (XmlTextReader reader, string sectionName)
  460. {
  461. string attName;
  462. string nameValue = null;
  463. string typeValue = null;
  464. string allowLoc = null, allowDef = null;
  465. bool requirePermission = false;
  466. string requirePer = null;
  467. bool allowLocation = true;
  468. AllowDefinition allowDefinition = AllowDefinition.Everywhere;
  469. while (reader.MoveToNextAttribute ()) {
  470. attName = reader.Name;
  471. if (attName == null)
  472. continue;
  473. if (attName == "allowLocation") {
  474. if (allowLoc != null)
  475. ThrowException ("Duplicated allowLocation attribute.", reader);
  476. allowLoc = reader.Value;
  477. allowLocation = (allowLoc == "true");
  478. if (!allowLocation && allowLoc != "false")
  479. ThrowException ("Invalid attribute value", reader);
  480. continue;
  481. }
  482. if (attName == "requirePermission") {
  483. if (requirePer != null)
  484. ThrowException ("Duplicated requirePermission attribute.", reader);
  485. requirePer = reader.Value;
  486. requirePermission = (requirePer == "true");
  487. if (!requirePermission && requirePer != "false")
  488. ThrowException ("Invalid attribute value", reader);
  489. continue;
  490. }
  491. if (attName == "allowDefinition") {
  492. if (allowDef != null)
  493. ThrowException ("Duplicated allowDefinition attribute.", reader);
  494. allowDef = reader.Value;
  495. try {
  496. allowDefinition = (AllowDefinition) Enum.Parse (
  497. typeof (AllowDefinition), allowDef);
  498. } catch {
  499. ThrowException ("Invalid attribute value", reader);
  500. }
  501. continue;
  502. }
  503. if (attName == "type") {
  504. if (typeValue != null)
  505. ThrowException ("Duplicated type attribute.", reader);
  506. typeValue = reader.Value;
  507. continue;
  508. }
  509. if (attName == "name") {
  510. if (nameValue != null)
  511. ThrowException ("Duplicated name attribute.", reader);
  512. nameValue = reader.Value;
  513. if (nameValue == "location")
  514. ThrowException ("location is a reserved section name", reader);
  515. continue;
  516. }
  517. ThrowException ("Unrecognized attribute.", reader);
  518. }
  519. if (nameValue == null || typeValue == null)
  520. ThrowException ("Required attribute missing", reader);
  521. if (sectionName != null)
  522. nameValue = sectionName + '/' + nameValue;
  523. reader.MoveToElement();
  524. object o = LookForFactory (nameValue);
  525. if (o != null && o != removedMark)
  526. ThrowException ("Already have a factory for " + nameValue, reader);
  527. SectionData section = new SectionData (nameValue, typeValue, allowLocation,
  528. allowDefinition, requirePermission);
  529. section.FileName = fileName;
  530. factories [nameValue] = section;
  531. if (reader.IsEmptyElement)
  532. reader.Skip ();
  533. else {
  534. reader.Read ();
  535. reader.MoveToContent ();
  536. if (reader.NodeType != XmlNodeType.EndElement)
  537. // sub-section inside a section
  538. ReadSections (reader, nameValue);
  539. reader.ReadEndElement ();
  540. }
  541. reader.MoveToContent ();
  542. }
  543. private void ReadRemoveSection (XmlTextReader reader, string sectionName)
  544. {
  545. if (!reader.MoveToNextAttribute () || reader.Name != "name")
  546. ThrowException ("Unrecognized attribute.", reader);
  547. string removeValue = reader.Value;
  548. if (removeValue == null || removeValue.Length == 0)
  549. ThrowException ("Empty name to remove", reader);
  550. reader.MoveToElement ();
  551. if (sectionName != null)
  552. removeValue = sectionName + '/' + removeValue;
  553. object o = LookForFactory (removeValue);
  554. if (o != null && o == removedMark)
  555. ThrowException ("No factory for " + removeValue, reader);
  556. factories [removeValue] = removedMark;
  557. MoveToNextElement (reader);
  558. }
  559. private void ReadSectionGroup (XmlTextReader reader, string configSection)
  560. {
  561. if (!reader.MoveToNextAttribute ())
  562. ThrowException ("sectionGroup must have a 'name' attribute.", reader);
  563. string value = null;
  564. do {
  565. if (reader.Name == "name") {
  566. if (value != null)
  567. ThrowException ("Duplicate 'name' attribute.", reader);
  568. value = reader.Value;
  569. }
  570. else
  571. if (reader.Name != "type")
  572. ThrowException ("Unrecognized attribute.", reader);
  573. } while (reader.MoveToNextAttribute ());
  574. if (value == null)
  575. ThrowException ("No 'name' attribute.", reader);
  576. if (value == "location")
  577. ThrowException ("location is a reserved section name", reader);
  578. if (configSection != null)
  579. value = configSection + '/' + value;
  580. object o = LookForFactory (value);
  581. if (o != null && o != removedMark && o != groupMark)
  582. ThrowException ("Already have a factory for " + value, reader);
  583. factories [value] = groupMark;
  584. if (reader.IsEmptyElement) {
  585. reader.Skip ();
  586. reader.MoveToContent ();
  587. } else {
  588. reader.Read ();
  589. reader.MoveToContent ();
  590. if (reader.NodeType != XmlNodeType.EndElement)
  591. ReadSections (reader, value);
  592. reader.ReadEndElement ();
  593. reader.MoveToContent ();
  594. }
  595. }
  596. // It stops XmlReader consumption at where it found
  597. // surrounding EndElement i.e. EndElement is not consumed here
  598. private void ReadSections (XmlTextReader reader, string configSection)
  599. {
  600. int depth = reader.Depth;
  601. for (reader.MoveToContent ();
  602. reader.Depth == depth;
  603. reader.MoveToContent ()) {
  604. string name = reader.Name;
  605. if (name == "section") {
  606. ReadSection (reader, configSection);
  607. continue;
  608. }
  609. if (name == "remove") {
  610. ReadRemoveSection (reader, configSection);
  611. continue;
  612. }
  613. if (name == "clear") {
  614. if (reader.HasAttributes)
  615. ThrowException ("Unrecognized attribute.", reader);
  616. factories.Clear ();
  617. MoveToNextElement (reader);
  618. continue;
  619. }
  620. if (name == "sectionGroup") {
  621. ReadSectionGroup (reader, configSection);
  622. continue;
  623. }
  624. ThrowException ("Unrecognized element: " + reader.Name, reader);
  625. }
  626. }
  627. void StorePending (string name, XmlTextReader reader)
  628. {
  629. if (pending == null)
  630. pending = new Hashtable ();
  631. pending [name] = reader.ReadOuterXml ();
  632. }
  633. private void ReadConfigFile (XmlTextReader reader)
  634. {
  635. //int depth = reader.Depth;
  636. for (reader.MoveToContent ();
  637. !reader.EOF && reader.NodeType != XmlNodeType.EndElement;
  638. reader.MoveToContent ()) {
  639. string name = reader.Name;
  640. if (name == "configSections") {
  641. if (reader.HasAttributes)
  642. ThrowException ("Unrecognized attribute in <configSections>.", reader);
  643. if (reader.IsEmptyElement)
  644. reader.Skip ();
  645. else {
  646. reader.Read ();
  647. reader.MoveToContent ();
  648. if (reader.NodeType != XmlNodeType.EndElement)
  649. ReadSections (reader, null);
  650. reader.ReadEndElement ();
  651. }
  652. } else if (name != null && name != "") {
  653. StorePending (name, reader);
  654. MoveToNextElement (reader);
  655. } else {
  656. MoveToNextElement (reader);
  657. }
  658. }
  659. }
  660. private void ThrowException (string text, XmlTextReader reader)
  661. {
  662. throw new ConfigurationException (text, fileName, reader.LineNumber);
  663. }
  664. #endif
  665. }
  666. }