Friday, March 30, 2007

Getting Multiple file extensions with Directory.GetFiles()

I was looking for a way to list the files in a folder with different extensions and I came across a solution that Andreas Kraus( wrote. So I took that and modified it a little

public static string[] GetFiles(
string path,
string searchPattern)
string[] m_arExt = searchPattern.Split(';');

List<string> strFiles = new List<string>();
foreach(string filter in m_arExt)
System.IO.Directory.GetFiles(path, filter));
return strFiles.ToArray();

Thanks Andreas.

Monday, March 26, 2007

SelectSingleNode with a default namespace

Ok, I have a simple xml file with a default namespace as shown here:

< test xmlns="">
< config>
< data>test< /data>
< settings>
< moredata>test< /moredata>
< /settings>
< /config>
< /test>

I loaded the xml file using
XmlTextReader reader = new XmlTextReader("C:\\xml\\My.Config.xml" );
nsm = new XmlNamespaceManager(reader.NameTable);

//I read that this is how you specify a default namespace
nsm.AddNamespace("", "");


and then I do a SelectSingleNode
XmlNode test2 = configDoc.SelectSingleNode("//Config");

This should work right? Wrong!!!
It turns out that XPath 1.0 doesn't support default namespace. XPath 2.0 does but I guess that doesn't help right now with .net 2.0.

What I needed to do was give the default namespace a name
nsm.AddNamespace("cfg", "");

Then all my selects need to specify this namespace
XmlNode test2 = configDoc.SelectSingleNode("//cfg:Config");

What a pain.