I know the easiest way to generate a config file is through visual studio. however the environment my program is going to be functioning in we are going to have several different configurations and the application needs to be able to build the config files on its own. Just curious if there is an easier way than making a large string literal and then copying over to a new file. Thanks for any help.
Not sure what kind of information you want to save in generated configurations.
If you are using only appSettings section which as only key values, then it would be better to generate a JSON file. It is very easy to generate it using newtonsoft.json.
in your app.config file you can keep the path of JSON file and load the settings at app startup if the file is already available.
NOTE:
JSON can also store any kind of complex configurations, you will have to generate the classes to hold those configurations.
Once you application puts value in these objects, serialize it to JSON and keep it in appropriate folder which is accessible to application.
Hope this helps.
I am working on a small windows forms program that reads data from a local database, which I have created by following this guide.
I have populated these tables with data using the Designer in Visual Studio, and there is no ability (nor will there ever be) for the program to make changes to this database at run-time, as they represent static, known data -- I could get identical results if I hard-coded the instantiation of each corresponding table row object in a constructor somewhere.
When I have Visual Studio build my solution, it generates two files -- the .exe that opens the Form, and a .mdf file with the database tables.
Two related questions -- does it even make sense to use a database with this kind of read-only data? And if so, is there a way to combine the .MDF file into the .exe? Again, there is zero need to ever modify the data, so I wouldn't think that the fact that you can't modify .exes need prevent this.
Yes you can do this. But first you need to construct your dataset. I would create your data in SQL or similar then export it to XML along with an XSD schema.
Then in Visual Studio, add a DataSet object to your project.
Then you can talk to the DataSet object and use the XSD and XML to populate it.
https://msdn.microsoft.com/en-us/library/atchhx4f(v=vs.110).aspx
To keep this all within the .EXE, embed your XML data into a Resource file, then access it via the Resources static class.
I want to add/remove/upsert a line for ADO .NET data provider programmatically with C#.
My first thought was to parse the file with some parser (like Eto.Parse), then add/remove necessary span of text and then write a new file into install image directory (which is not write protected unlike to write protected main machine.config).
Then I think, that the file is xml, and it is possible to use existing xml machinery instead of custom parser. Load XML, build object model from XML, modify it and serialize.
Then I realise, that object model for working with configs is already present in System.Configuraion namespace.
And I decide to search an existing example on how to modify the machine config with these classes. I found only an example how to obtain it's location new ConfigurationFileMap().MachineConfigFilename; (see The best way to get a path to machine.config of a different .NET version)
Just tell the ConfigManager that you are looking to edit something other than the current app's config file.
Configuration config = ConfigurationManager.OpenMachineConfiguration();
The, you can use config.sections[whatever] to access specific sections.
Keep in mind that the config object maps out most of the properties you are attempting to tweak, so you'll need to dig through the specific section's interface to find exactly what you're trying to mess-up update.
I have a xml file (that i created in c# using XmlDocument) and now i have to create xsd file the corresponds to it also programmatically in C# ... how can I do this ??
can I use the same classes for creating xml files ? or are there any other (xsd related) classes I should use ??
If you just want to produce an XSD file from an XML file, then you can use Microsoft's XSD.EXE, which is included with Visual Studio.
To use it, open a Visual Studio Command Prompt. Then enter xsd file.xml to produce a schema for that XML file.
You can also use that tool to generate classes for your XML input, just pass in the /classes parameter.
While an XML Schema file is an XML file, it has certain things that could make it cumbersome to do it "by hand"; one could say why write XML using the DOM API instead of using C# classes generated by XSD.exe or XSD2Code.exe? Or to push it a bit... somewhat similar to someone saying C# statements eventually turn into IL assembly; why not write IL instead?
Another alternative is provided by the Schema Object Model API; in .NET, it is the System.Xml.Schema namespace.
Take a look at the code example found here on MSDN. It'll give you an idea for another approach. It provides a programmer friendly API to generate XSDs, instead of dealing with the actual XML.
XSD is just another XML-type file. whatever you are using to create your XML file will also be useful to create the XSD file; you just need to know what tags/attributes you want to put in the file...
Solution:
I originally posted a reply, but Stack Overflow rather I edit my response, so here is the edit. The original tool that I suggested goes the other way. I then did some research and someone recommended a tool called MyGenerations or something like that. That required an installation, downloaded template, and oodles of work, so way too complicated. I then did some more research and came across a rather nifty solution on CodePage, but that solution, which works, because I tested it, requires some modifications. The code is called XmlToXsd with the URL:
http://www.codeproject.com/Articles/133570/XmlToXsd-A-Better-Schema-Generator.
The Programs section has two bugs. First it saves the XSD to the executable's root. Do not worry, the exe merely calls a method located in one CS file. Simply include that CS file in your project and call with the line given, just you have to change the path to the same folder as the XML. Additionally, the author uses a targetnamespace of a junk URL. DevExpress throws a complaint on the nonexistent URL. Through some effort, I replaced the target URL with null. In the accompanying CS file I had to make a few changes. Others with more understanding than I can modify to his/her hearts extent. I merely help others by telling them what I did, so no static back.
Change 1: Before "target = XNamespace.Get(targetNamespace);" add an if-statement only loading if not null for the target namespace.
Change 2: I added another if-statement for the return. The null return, a copy of the original return modifies as follows:
2.1: Nuke the targetnamespace attribute item.
2.2: After the replace call, add another replace call of ".Replace("xmlns=\"null\"", null))".
2.3: Before the return, requires adding braces, set target = "null".
The resulting file now appears totally valid and created programatically.
Yes, one can use XSD.exe but manually, but I needed a programatic way of the conversation.
Original Post:
I had the same question, not necessarily programatically. The XSD utility does do the job. The problem is that Microsoft does not distribute xsd.exe with Visual Studio 2012 Professional. I searched my hard drive and found the utility with Microsoft's SDK, path on my disk "C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\x64\xsd.exe".
I had to go to the folder where the XML file is and execute XSD utility from a command box (not in Visual Studio) and wound up with the XSD file.
One way to do this task programatically is to call XSD from code, but that would mean distributing XSD.
I found this one assembly, but never used it that might do the job. Check out: http://xsd2code.codeplex.com/
Try this:
string xmlFilePath = #"myxmlfile.xml";
string xsdOutputPath = #"myxmlfile.xsd";
DataSet ds = new DataSet();
System.IO.FileStream fsReadXml = new System.IO.FileStream(xmlFilePath, System.IO.FileMode.Open);
ds.ReadXml(fsReadXml);
ds.WriteXmlSchema(xsdOutputPath);
If from xDocument:
DataSet ds = new DataSet();
ds.ReadXml(YourXDocument.CreateReader());
ds.WriteXmlSchema(xsdOutputPath);
I'm writing an API for NOAA forecast data, and I'm trying to get the information from a XML document like this into a de-serialized object. I Downloaded the schema and it's two dependacies (meta_data.xsd & ndfd_data.xsd ) and tried running this command
svcutil C:\DWML.xsd /dconly
But it returns with
Error: Cannot read D:\DWML.xsd.
Cannot load file D:\DWML.xsd as an Assembly. Check the FusionLogs for more information.
Can someone please walk me through the steps to create data contract for this xml document?
If there is an easier way to get this information into a easily querable form that doesn't require Data Contracts I'm willing to change my approach.
You need to supply all the xsd files to SvcUtil like so
svcutil *.xsd /dconly
However trying this with NOAA schema gives bunch of errors and it suggested to use /importXmlTypes. But the following did not work for me either:
svcutil *.xsd /dconly /ser:XmlSerializer /importXmlTypes
Finally, used Xsd2Code
xsd2Code.exe DWML.xsd
and it worked like a charm.