I have the following function:
public static XmlNode GetXMLNodeFromString(string strXML)
{
XmlDocument doc = new XmlDocument();
doc.LoadXml(strXML);
return doc.DocumentElement;
}
which can be called a million times in a line of code that gets returned from a database call:
while (reader.Read())
{
myXMLList.Add(GetXMLNodeFromString((string)reader["GMLString"]));
}
Is there a better way than to keep instantiating the xmlDocument for every row? Or is it fine to do so?
I don’t want to this:
XmlDocument doc = new XmlDocument();
while (reader.Read())
{
myXMLList.Add(doc, GetXMLNodeFromString((string)reader["GMLString"]));
}
because in all reality there is a tree of functions that i would have to add it to. Not just GetXMLNodeFromString.
Can i do something like this:
public static class Common
{
public static XmlDocument doc = new XmlDocument();
public static XmlNode GetXMLNodeFromString(string strXML)
{
doc.LoadXml(strXML);
return doc.DocumentElement;
}
}
What should i do?
I recommend XmlReader if you’re having performance problems, as it allows you to parse the XML as it comes in, rather than pre-parsing it and creating an in-memory object model that you’re going to have to go back through again anyway. It can require restructuring your code a bit, however, as it gives you sequential (as opposed to random) access. If you don’t need random access, it’s a good choice.