@@ -358,25 +358,20 @@ private static void LogAndThrow(Exception exception)
358
358
public Configuration AddXmlFile ( string xmlFile )
359
359
{
360
360
log . Info ( "Mapping file: " + xmlFile ) ;
361
- XmlTextReader textReader = null ;
362
- try
363
- {
364
- textReader = new XmlTextReader ( xmlFile ) ;
365
- AddXmlReader ( textReader , xmlFile ) ;
366
- }
367
- catch ( MappingException )
368
- {
369
- throw ;
370
- }
371
- catch ( Exception e )
361
+ using ( TextReader streamReader = File . OpenText ( xmlFile ) )
362
+ using ( XmlReader textReader = XmlReader . Create ( streamReader ) )
372
363
{
373
- LogAndThrow ( new MappingException ( "Could not configure datastore from file " + xmlFile , e ) ) ;
374
- }
375
- finally
376
- {
377
- if ( textReader != null )
364
+ try
365
+ {
366
+ AddXmlReader ( textReader , xmlFile ) ;
367
+ }
368
+ catch ( MappingException )
378
369
{
379
- textReader . Close ( ) ;
370
+ throw ;
371
+ }
372
+ catch ( Exception e )
373
+ {
374
+ LogAndThrow ( new MappingException ( "Could not configure datastore from file " + xmlFile , e ) ) ;
380
375
}
381
376
}
382
377
return this ;
@@ -400,28 +395,23 @@ public Configuration AddXml(string xml, string name)
400
395
{
401
396
log . Debug ( "Mapping XML:\n " + xml ) ;
402
397
}
403
- XmlTextReader reader = null ;
404
- try
405
- {
406
- reader = new XmlTextReader ( xml , XmlNodeType . Document , null ) ;
407
- // make a StringReader for the string passed in - the StringReader
408
- // inherits from TextReader. We can use the XmlTextReader.ctor that
409
- // takes the TextReader to build from a string...
410
- AddXmlReader ( reader , name ) ;
411
- }
412
- catch ( MappingException )
398
+ using ( TextReader textReader = new StringReader ( xml ) )
399
+ using ( XmlReader reader = XmlReader . Create ( textReader ) )
413
400
{
414
- throw ;
415
- }
416
- catch ( Exception e )
417
- {
418
- LogAndThrow ( new MappingException ( "Could not configure datastore from XML string " + name , e ) ) ;
419
- }
420
- finally
421
- {
422
- if ( reader != null )
401
+ try
402
+ {
403
+ // make a StringReader for the string passed in - the StringReader
404
+ // inherits from TextReader. We can use the XmlTextReader.ctor that
405
+ // takes the TextReader to build from a string...
406
+ AddXmlReader ( reader , name ) ;
407
+ }
408
+ catch ( MappingException )
409
+ {
410
+ throw ;
411
+ }
412
+ catch ( Exception e )
423
413
{
424
- reader . Close ( ) ;
414
+ LogAndThrow ( new MappingException ( "Could not configure datastore from XML string " + name , e ) ) ;
425
415
}
426
416
}
427
417
return this ;
@@ -634,27 +624,21 @@ public Configuration AddInputStream(Stream xmlInputStream)
634
624
/// </remarks>
635
625
public Configuration AddInputStream ( Stream xmlInputStream , string name )
636
626
{
637
- XmlTextReader textReader = null ;
638
- try
639
- {
640
- textReader = new XmlTextReader ( xmlInputStream ) ;
641
- AddXmlReader ( textReader , name ) ;
642
- return this ;
643
- }
644
- catch ( MappingException )
645
- {
646
- throw ;
647
- }
648
- catch ( Exception e )
627
+ using ( XmlReader textReader = XmlReader . Create ( xmlInputStream ) )
649
628
{
650
- LogAndThrow ( new MappingException ( "Could not configure datastore from input stream " + name , e ) ) ;
651
- return this ; // To please the compiler
652
- }
653
- finally
654
- {
655
- if ( textReader != null )
629
+ try
630
+ {
631
+ AddXmlReader ( textReader , name ) ;
632
+ return this ;
633
+ }
634
+ catch ( MappingException )
656
635
{
657
- textReader . Close ( ) ;
636
+ throw ;
637
+ }
638
+ catch ( Exception e )
639
+ {
640
+ LogAndThrow ( new MappingException ( "Could not configure datastore from input stream " + name , e ) ) ;
641
+ return this ; // To please the compiler
658
642
}
659
643
}
660
644
}
@@ -1462,19 +1446,11 @@ private Configuration Configure(string fileName, bool ignoreSessionFactoryConfig
1462
1446
properties = Environment . Properties ;
1463
1447
}
1464
1448
1465
- XmlTextReader reader = null ;
1466
- try
1449
+ using ( TextReader streamReader = File . OpenText ( fileName ) )
1450
+ using ( XmlReader reader = XmlReader . Create ( streamReader ) )
1467
1451
{
1468
- reader = new XmlTextReader ( fileName ) ;
1469
1452
return Configure ( reader ) ;
1470
1453
}
1471
- finally
1472
- {
1473
- if ( reader != null )
1474
- {
1475
- reader . Close ( ) ;
1476
- }
1477
- }
1478
1454
}
1479
1455
1480
1456
/// <summary>
@@ -1507,7 +1483,7 @@ public Configuration Configure(Assembly assembly, string resourceName)
1507
1483
+ " in Assembly " + assembly . FullName ) ;
1508
1484
}
1509
1485
1510
- return Configure ( new XmlTextReader ( stream ) ) ;
1486
+ return Configure ( XmlReader . Create ( stream ) ) ;
1511
1487
}
1512
1488
}
1513
1489
0 commit comments