I have an asp.net application and I'm using C#. I want to use the XmlDocument.LoadXml() method to read from an .xml file. However, the xml file will not always have the same name so I wanted to pass into the LoadXml() method the path to the file and then read any .xml files that are inside. So, something like this LoadXml(C:\Docs*.xml). It doesn't work for me. Is there another way I can accomplish this?
+2
A:
You need to separate out the "loading XML from a file" from "picking which file to load". The two are unrelated concepts. (Although I would point out that XmlDocument.LoadXml
takes raw XML as a string, not a filename. I think you want XmlDocument.Load
.)
What do you want to happen if there's more than one XML document in c:\Docs
? XmlDocument
can only load one of them.
Use Directory.GetFiles(@"C:\Docs", "*.xml")
to get the list of matching files in the directory. What you should do if there's more than one of them (or none) is up to you.
Jon Skeet
2009-06-06 19:14:44
Hey thanks, there should only be one in the directory and it gets overwritten.
jumbojs
2009-06-06 19:16:19
Then I would make that very explicit in the code - if there isn't exactly one appropriate file, you should probably throw an exception. Glad it helped though.
Jon Skeet
2009-06-06 19:17:00