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
Related
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.
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.
I'm fairly new to coding, and I just got help figuring out how to create a Xml file; now I want to know, is there a way to protect my Xml file from being edited?
I'm making a simple Command Prompt game, and I'm going to include an Xml file for info storage purposes. Although I don't want the user to be able to change the info contained in the file.. Is there a way to achieve this? It doesn't need to be extensive at this time, due to the program only being a small project.
Anyway, I'm making the program with Visual Studio Pro 2010, and I'm coding it in C#.
Thank you, for any help in advance.
the standard way to verify that parts of your xml has not been modified is to use XML_Signature
this msdn example shows how this is done with dotnet4
I would embed your XML file as a resource of your console application's assembly. The XML file will exist as an embedded resource and not as a seperate file that the user could potentially change. If the user isn't meant to edit a configuration file, don't even let him see it, modify it, or delete it.
look at this topic decrypt and encrypt
i have created my own Encrypter class based from this classes. then you can create it for yourself for next use
You could simply compress it, if you don't need a high level of security. You could use a standard format (ZIP, CAB), or just deflate the stream and store it as a binary file. See the doc and examples about this here: DeflateStream Class
You can't prevent anyone from editing your xml file but you can encrypt your xml file to protect your data.
I am developing a WPF client program for some websites. It uses XML database. I am new to XML. Would someone please explain how to create,append(Most important),edit,read&encrypt XML file. It is a big question,i know . But, it is urgent.Have to complete the work ASAP. Searched in the internet, not getting correct info.
You should seriously consider using a DataSet within your application and load up your data from an XML file via DataSet,ReadXml. When you're done with your updates write your changes using DataSet.WriteXml.
But you should also seriously consider not using XML as a database.
Here's an article on CodeProject that discusses using XML as a database:
Xml Database Demo
I know you tagged this question C# but unfortunately the demo app is written in VB.NET.
(in response to your comment on Gerri's answer)
XML is inherently not appendable. A valid XML document requires a single document element. In order to "append" you would need to be able to back over the closing tag of the document element and overwrite it. The only option is to read in the entire document and write it back out again. Also you may want to use XmlDocument or XDocument instead of XmlWriter which is a horribly painful API when you don't need very fine grained control over the behavior.
The fact is, XML makes a really terrible database format. There's other lightweight database solutions out there that don't require a database server.
Assuming your database is small enough that you can easily load it into memory.
Create classes that model your database.
Add DataContract attributes to them to indicate how you want them serialized.
Use DataContractSerializer to serialize your database to XML and then save it to disk.
Each time you update the database:
Create new file as .tmp
Delete any old file called .old
Rename .xml to .old
Rename new file from .tmp to .xml
When you go to load the file, if .xml is corrupt or missing, try .tmp
This will help you survive the inevitable corruption that will occur during writing when something goes wrong.
Due to history of each data base company coming up with a "standard" interface that no other company follows, XML has become the defacto way to transfer data between databases.
If this in the intended use than it is fine as it only has to write in this format some times. There is a lot to worry about in writting XML using .NET as it has a lot of ways to forget to finish the writing leaving open tags (always use using/flush/close). Warning: The more processing cores the more often .Net screws up. Use Thread.BeginCriticalRegion()/Thread.EndCriticalRegion() if you have more than four real cores. Also as suggested it is best to save the earlier version as a .bak or such.
Of course if the XML standards could have a declaration of "document set" then we could append a document each time and life would be a lot easier.
Load your XML as an XMLDocument.
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.