M
Mitch
Hello,
I have a singleton class that loads a config file (for an assembly) into an
internal stream object. I designed this class as singleton because it
parses the config file many times and I do not want to load the config file
everytime. The problem is I am using XMLReader because of speed and after I
read an item (multiple nodes so pass node name and element name) and try to
read another item I get an error "root element missing". Is the error
caused by XMLReader object or should I change this a load the config file
into XMLDocument and parse it. Am I even acheiving that much performance
gain using XMLReader? Any ideas?
public string GetXMLValue(string nodeName, string elementName)
{
string NS = "";
string xmlValue = "";
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.ProhibitDtd = true;
settings.IgnoreComments = true;
settings.IgnoreProcessingInstructions = true;
settings.ConformanceLevel = ConformanceLevel.Document;
XmlReader reader = null;
using (reader = XmlReader.Create(mappingStream, settings))
{
try
{
reader.Read();
reader.ReadToDescendant(nodeName, NS);
while (reader.ReadToFollowing(elementName, NS))
{
xmlValue = reader.ReadElementContentAsString();
}
return xmlValue;
}
catch
{
return xmlValue;
.....
}
}
}
I have a singleton class that loads a config file (for an assembly) into an
internal stream object. I designed this class as singleton because it
parses the config file many times and I do not want to load the config file
everytime. The problem is I am using XMLReader because of speed and after I
read an item (multiple nodes so pass node name and element name) and try to
read another item I get an error "root element missing". Is the error
caused by XMLReader object or should I change this a load the config file
into XMLDocument and parse it. Am I even acheiving that much performance
gain using XMLReader? Any ideas?
public string GetXMLValue(string nodeName, string elementName)
{
string NS = "";
string xmlValue = "";
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
settings.ProhibitDtd = true;
settings.IgnoreComments = true;
settings.IgnoreProcessingInstructions = true;
settings.ConformanceLevel = ConformanceLevel.Document;
XmlReader reader = null;
using (reader = XmlReader.Create(mappingStream, settings))
{
try
{
reader.Read();
reader.ReadToDescendant(nodeName, NS);
while (reader.ReadToFollowing(elementName, NS))
{
xmlValue = reader.ReadElementContentAsString();
}
return xmlValue;
}
catch
{
return xmlValue;
.....
}
}
}