Skip to content

Commit 21c8825

Browse files
committed
NH-3807 - Use XmlReader.Create instead of XmlTextReader.
XmlTextReader has been removed from CoreClr
1 parent 225d8e0 commit 21c8825

File tree

2 files changed

+49
-69
lines changed

2 files changed

+49
-69
lines changed

src/NHibernate/Cfg/Configuration.cs

Lines changed: 43 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -358,25 +358,20 @@ private static void LogAndThrow(Exception exception)
358358
public Configuration AddXmlFile(string xmlFile)
359359
{
360360
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))
372363
{
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)
378369
{
379-
textReader.Close();
370+
throw;
371+
}
372+
catch (Exception e)
373+
{
374+
LogAndThrow(new MappingException("Could not configure datastore from file " + xmlFile, e));
380375
}
381376
}
382377
return this;
@@ -400,28 +395,23 @@ public Configuration AddXml(string xml, string name)
400395
{
401396
log.Debug("Mapping XML:\n" + xml);
402397
}
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))
413400
{
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)
423413
{
424-
reader.Close();
414+
LogAndThrow(new MappingException("Could not configure datastore from XML string " + name, e));
425415
}
426416
}
427417
return this;
@@ -634,27 +624,21 @@ public Configuration AddInputStream(Stream xmlInputStream)
634624
/// </remarks>
635625
public Configuration AddInputStream(Stream xmlInputStream, string name)
636626
{
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))
649628
{
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)
656635
{
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
658642
}
659643
}
660644
}
@@ -1462,19 +1446,11 @@ private Configuration Configure(string fileName, bool ignoreSessionFactoryConfig
14621446
properties = Environment.Properties;
14631447
}
14641448

1465-
XmlTextReader reader = null;
1466-
try
1449+
using (TextReader streamReader = File.OpenText(fileName))
1450+
using (XmlReader reader = XmlReader.Create(streamReader))
14671451
{
1468-
reader = new XmlTextReader(fileName);
14691452
return Configure(reader);
14701453
}
1471-
finally
1472-
{
1473-
if (reader != null)
1474-
{
1475-
reader.Close();
1476-
}
1477-
}
14781454
}
14791455

14801456
/// <summary>
@@ -1507,7 +1483,7 @@ public Configuration Configure(Assembly assembly, string resourceName)
15071483
+ " in Assembly " + assembly.FullName);
15081484
}
15091485

1510-
return Configure(new XmlTextReader(stream));
1486+
return Configure(XmlReader.Create(stream));
15111487
}
15121488
}
15131489

src/NHibernate/Cfg/ConfigurationSchema/HibernateConfiguration.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.IO;
23
using System.Xml;
34
using System.Xml.XPath;
45

@@ -59,8 +60,11 @@ private HibernateConfiguration(XmlReader hbConfigurationReader, bool fromAppSett
5960

6061
internal static HibernateConfiguration FromAppConfig(XmlNode node)
6162
{
62-
XmlTextReader reader = new XmlTextReader(node.OuterXml, XmlNodeType.Document, null);
63-
return new HibernateConfiguration(reader, true);
63+
using (TextReader textReader = new StringReader(node.OuterXml))
64+
using (XmlReader reader = XmlReader.Create(textReader))
65+
{
66+
return new HibernateConfiguration(reader, true);
67+
}
6468
}
6569

6670
private XmlReaderSettings GetSettings()

0 commit comments

Comments
 (0)