How to convert app.config file to XML? - c#

I use this to get the content from the app.config file:
string content = File.ReadAllLines(
AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
How can I convert app.config file into XML after parsing config this way? What are the best practices for parsing?

Unless you are doing something really special with it, you should not directly read the app.config file, but use ConfigurationManager class to read from it.
If you really want to read it as XML, use XmlDocumen.Load() function, but plaease keep in mind that parsing config as an XML file is an unintended way of use.

Related

How to convert Visio document .xml to csv file c#

I have multiple xml files, this files generated from convert .vsdx "Visio File" to .xml file, Now i want to convert the generated files from .xml to .csv files.
My problem the files has hundreds lines and the xml shape tag can't be followed to extract csv file using xml tag.
I work with this tool but the problem the out structure very complex
there is any way to make that ?
Normally one solves this with XSLT. If you want to create a csv file programmatically I would query the xml file with Linq to Xml append each record as a csv formatted line into a StringBuilder instance and finally write the StringBuilders content into the target file.
I haven't worked with Visio based XML files but given that the XML'ishness is alike across any source (which it should be), I'd go LINQ2XML or, depending on what you feel convenient with read in the stuff as a XDocument (avoid XmlDocument, as it's older and obsolete) and then parse it to a String.
It depends a bit on how complex files are and how invasive operations that you'll need to carry out.
As for the tool you mention, I haven't seen it before but it could be a better idea not to use it as it strikes me as a bit outdated. However, I only glanced at it without scrutinizing. If you only need to see the data for yourself (human based analysis of the contents), you might perhaps use a decent text editor with some appropriate plugin (such as Notepad++ and its XML add-on).

Save as DictionarySectionHandler to App.config file

I have several sections on my App.config file, saved as DictionarySectionHandlers. I can easily read them by casting them to a hashtable. Now, what I want, is to also be able to save a hashtable/dictionary as a DictionarySectionHandler, back to the App.config file. How is that possible?

How to hold XML file and get the fresh info from him every time i need

I want to have pointer to xml file and when ever i need to read from him some info i will go directly to the place in this xml file and bring the fresh info.
How can i hold this xml ?
I need to give the ability to change the info in this file also.
You can use the FileSystemWatcher class to detect whenever the XML file gets modified (and react by reloading it). The example on MSDN is quite instructive.
You can't really have a "pointer to xml file" as such. I suggest using XmlReader and XmlWriter classes for reading / writing the XML if the file is expected to be rather big, or LINQ to XML and the XElement class if its size is more likely to be moderate.
If you come across any particular implementation problems, specify them.

How to parse web.config file in c#

I have web.config file from some application. It is located in some random location. I have to parse this web.config file (get all keys names and values). I tried to use ConfigurationManager class in order to get those data however, it throws exception when I try to get some Sections (Configuration->GetSection('section name')). It throws exception because I do not have dll that this section points to (because I have only web.config not whole application). It seems that GetSection method check underlying dll in order to get more info, but I just need value (name of dll).
What can I do, to turn off this mechanism, do you know other simple solutions to get it done ?
You are just going to have to use XmlDocument or XDocument (3.5) to parse the file.
If you just want to read the text, and not do any web.config-specific processing, use the fact that a .config file is XML, and use your favourite usual way of reading and parsing XML.
Web.Config files are just XML and an be read using a number of .Net XML objects. Below are a couple of methods.
Tutorial on reading an XML file using XmlTextReader http://support.microsoft.com/kb/307548
Tutorial on reading an XML file using LinqToSQL http://www.mssqltips.com/tip.asp?tip=1524

C# XmlDocument.LoadXml And Wildcards

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?
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.

Categories

Resources