xml "file handle" to share

P

phl

hi,

I have created nice factory/product classes to parse a big xml file and
create a custom obj. i plan to instantiate the opening of the xml file
in the factory and have the actual parse code in the product classes.
So several "products" are going to use the different sections of the
XML file at different stages of execute. What would be the nicest way
to ensure that the XmlReaderClass is only opened once and whats the
best way to pass it around?

I have came up with a static variable solution but is there a nicer
way?

thx...here's some code:

class LoadXML
{
static XmlTextReader reader;

public LoadXML()
{

}

public static void OpenXMLfile()
{

if (reader.ReadState == ReadState.Closed)
{
reader = new
XmlTextReader(@"D:\philip\samples\xml\FinXML_US_MSFT.xml");
}
}
}
 
N

Nicholas Paldino [.NET/C# MVP]

phl,

Why not have the interface to your classes take the XmlReader as a
parameter in the method that parses the XML?

This way, you don't have to deal with getting it from a static location,
and if you need to change where the file is read from between separate
instances of classes, it won't cause a big fuss.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top