1

I have a list of XmlTextReaders from the System.Xml namespace like so

        List<XmlTextReader> test = new List<XmlTextReader>{};

that then has elements added to it like so

IEnumerable<string> Files = Directory.GetFiles(ConfigurationManager.AppSettings.Get("TestDirectory"));

if (Files.Count() == 0) //the check for existance and the catch aren't included in this snippet
    throw new DirectoryNotFoundException("The directory exists, however it is empty.");
foreach(var file in Files)
{
    test.Add(new XmlTextReader(file));
}

Once I have passed this list to another class, is there a way for me to retrieve the file name/path?

I have searched through the MSDN class information and as far as I can tell there is nothing of the sort, but I'm hoping I just missed it.

If it is not possible, I will instead pass the files and not the XmlTextReaders, in order to maintain the paths and just open them into readers later. But I'm hoping there is a property/method of XmlTextReader that I am just misunderstanding that will give me the file name or path (I don't even need the whole path, just the name.)

Here is the MSDN article on the XmlTextReader class.

2 Answers 2

1

You cannot directly.

The XmlTextReader class doesn't even store the URL itself. It instantiates an inner class called XmlTextREaderImpl which does store the URL, but it is not exposed through any property and even the impl class itself is not exposed either.

You can however create your own wrapper. Create a new class that extends XmlTextReader, accepts the url as its constructor argument, stores it, exposes it as a property, and delegates everything else to an internal XmlTextReader.

If you have ReSharper it's even easier to write a class like this using ReSharper's "Generate Delegating Members" feature.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! This is exactly the sort of answer I was looking for. Wrapper it is.
1

Another solution using somewhat similar code to what you have:

    Dictionary<XmlTextReader, string> test = new Dictionary<XmlTextReader, string>{};

that then has elements added to it like so

IEnumerable<string> Files = Directory.GetFiles(ConfigurationManager.AppSettings.Get("TestDirectory"));

if (Files.Count() == 0) //the check for existence and the catch aren't included in this snippet
    throw new DirectoryNotFoundException("The directory exists, however it is empty.");
foreach(var file in Files)
{
    test.Add(new XmlTextReader(file), file);
}

Then you can either iterate over the test Dictionary or find the file name by the XmlTextReader key.

1 Comment

Oh, this is a neat solution too. I'm still relatively new to c# and never considered a dictionary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.