September 12, 2013 at 4:28 am
I have been on working windows console application to import xml data into database.
I am new to console applications,
I have a task i.e i want to import xml data from folders(which are created and saved xml files ex:c:\Projects\XMLfolder\folder\abc.xml, c:\Projects\XMLfolder\folder1\xyz.xml,)
for this i don't want to hard code, the file path for this just add key/value in the app.config file. Based on file path how to read xml files one by one(if exist file folder) if not exist xml file in current folder go next folder read the file if exist, after completion of reading file in a folder file need to move into another folder based on path mentioned in app.config.
How would i achieve this?
September 13, 2013 at 7:22 am
Can you post more details? Sounds like you're using .NET (since you mention app.config) but knowing what language you're coding would be helpful (e.g. c#? VB.NET?) as would what version of .NET you're using.
That said, what you're asking about is pretty standard functionality in .NET. For the app.config part, just add your XML file paths as appSettings in the app.config file, then read them back out.
http://msdn.microsoft.com/en-us/library/aa735785(v=vs.71).aspx
http://forums.asp.net/t/1168194.aspx
This isn't the only way to handle configuration data, but it's the simplest and it's baked right into .NET and might serve your needs. If you want to put a variable set of paths in your config then you might be better off with a custom configuration section, but that isn't necessarily needed depending on how you set up your appSetting (e.g. if you have a setting that says how many path elements you have, and you number the actual path elements, you could handle it "by convention").
OK, that's for setting up the app.confiig. Now how to actually read it and process your XML files? Do you already know how to open/read an XML file in .NET? Xml-handling code is in the System.Xml and related assemblies -- when you start a new console app project in Visual Studio, System.Xml should be automatically added to project references for you.
To open and process your files you have a couple of options: XmlDocument and XmlReader. Read up on these regarding how and when to use them. Basically, you load up a document and then read through it, and process the data in the document however you need to do so, to meet your requirements. If yo're dealing with huge documents, you'll want to use XmlReader. For small/simle documents, XmlDocument is probably better.
You don't say what you intend to do with the XML documents, but note that if you're just wanting to load the data into a database then using an app to do it isn't your only choice (and may not be the best choice either).
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply