/
ConfigXmlDocument.cs
128 lines (110 loc) · 3.97 KB
/
ConfigXmlDocument.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Configuration.Internal;
using System.IO;
using System.Xml;
namespace System.Configuration
{
// ConfigXmlDocument - the default Xml Document doesn't track line numbers, and line
// numbers are necessary to display source on config errors.
// These classes wrap corresponding System.Xml types and also carry
// the necessary information for reporting filename / line numbers.
// Note: these classes will go away if webdata ever decides to incorporate line numbers
// into the default XML classes. This class could also go away if webdata brings back
// the UserData property to attach any info off of any node.
public sealed class ConfigXmlDocument : XmlDocument, IConfigErrorInfo
{
private XmlTextReader _reader;
private int _lineOffset;
private string _filename;
int IConfigErrorInfo.LineNumber
{
get
{
if (_reader == null)
{
return 0;
}
if (_lineOffset > 0)
{
return _reader.LineNumber + _lineOffset - 1;
}
return _reader.LineNumber;
}
}
public int LineNumber { get { return ((IConfigErrorInfo)this).LineNumber; } }
public string Filename
{
get { return _filename; }
}
string IConfigErrorInfo.Filename
{
get { return _filename; }
}
public override void Load(string filename)
{
_filename = filename;
try
{
_reader = new XmlTextReader(filename);
_reader.XmlResolver = null;
base.Load(_reader);
}
finally
{
if (_reader != null)
{
_reader.Close();
_reader = null;
}
}
}
public void LoadSingleElement(string filename, XmlTextReader sourceReader)
{
_filename = filename;
_lineOffset = sourceReader.LineNumber;
string outerXml = sourceReader.ReadOuterXml();
try
{
_reader = new XmlTextReader(new StringReader(outerXml), sourceReader.NameTable);
base.Load(_reader);
}
finally
{
if (_reader != null)
{
_reader.Close();
_reader = null;
}
}
}
public override XmlAttribute CreateAttribute(string prefix, string localName, string namespaceUri)
{
return new ConfigXmlAttribute(_filename, LineNumber, prefix, localName, namespaceUri, this);
}
public override XmlElement CreateElement(string prefix, string localName, string namespaceUri)
{
return new ConfigXmlElement(_filename, LineNumber, prefix, localName, namespaceUri, this);
}
public override XmlText CreateTextNode(string text)
{
return new ConfigXmlText(_filename, LineNumber, text, this);
}
public override XmlCDataSection CreateCDataSection(string data)
{
return new ConfigXmlCDataSection(_filename, LineNumber, data, this);
}
public override XmlComment CreateComment(string data)
{
return new ConfigXmlComment(_filename, LineNumber, data, this);
}
public override XmlSignificantWhitespace CreateSignificantWhitespace(string data)
{
return new ConfigXmlSignificantWhitespace(_filename, LineNumber, data, this);
}
public override XmlWhitespace CreateWhitespace(string data)
{
return new ConfigXmlWhitespace(_filename, LineNumber, data, this);
}
}
}