Using SVCUTIL.EXE to generate DataContracts for NOAA Forcast - c#

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.

Related

Generating C# class from XML or XSD programatically

I have a backend application in production and need to update XSD and C# classes continuously after every update of XMLs that the application is receiving. I'm trying to write a C# code which I can run and it will automatically update both XSD and C# classes so I don't have to overwrite it by myself (there comes many new or updated fields each month and the validation is needed).
I managed to write a code that generates XSD from new XML but haven't seen any (open source) utility which would be able to create a C# class of objects based on one of these. I know I could use following:
Process.Start("....xsd.exe", params)
However, the solution should be generic for more users and the path of xsd.exe is not same for everyone.
Have you got any suggestions on how should I deal with that?
After all I managed to write a generic .bat file which calls xsd.exe upon all needed xsds like so
for /R %%f in (*.xsd) do (
xsd /c "%%f" /outputdir:..\Objects /n:DataSource.Main.xsd.Objects)
and run this file programatically. The only con of this solution is that you need to add xsd.exe to your PATH.

What is the easiest way to generate an App.Config file through pure c# code

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.

Generating C# client code from a JAXB WSDL with a binding file

I have a WSDL with dozens of XSD and I need to generate client code from the WSDL to consume a Web Service. The owner of the web service uses Java and generated the WSDL/XSD using JAXB. JAXB specifies a "binding file" for customized bindings, and the owner has generated a XJB file (JAXB customized binding file) to be used with the WSDL.
I need to generate my client code in C#, but svcutil and Visual Studio's Add Service Reference don't have any provisions for this customized bindings file. There are tools to generate Java client code that take a binding file as an argument, but no tools for C# generation. I've trying just running svcutil without specifying the XJB, but I can't get it to work. I've done extensive Google searches with no luck. Any ideas on how to make this work?
You can customize WSDLs by providing binding declarations, but these are Java specific and the customizations apply only while generating Java code from the WSDL. The bindings can be declared inline (embedded in the XSD/WSDL) or within a separate file.
You have a separate file so I'm thinking the WSDL and XSDs are "clean".
The JXB file is a red herring though. A WSDL is supposed to be technology agnostic, it doesn't care about Java, C# or whatever. You should be able to generate the code because svcutil will just ignore the Java bindings.
What error does svcutil throw at you? I've seen it fail with hard to understand messages when the WSDL was missing a <type> section. You mentioned dozens of XSD files, how are they imported in the WSDL and does the import work? If you open the WSDL in Visual Studio does it show validation errors, is it complaining about something?
Finally, if everything fails, you could feed it all (including the JXB file) to a Java tool like wsimport and get some Java files out of it. You can do a quick deploy with Endpoint.publish and then point svcutil to the deployed address plus ?wsdl and see if you get a different result.
Try out WSCF.blue and see if that is any useful in your case. Please note I am not familiar with JABX, but I am using this tool currerntly to generate code in C# from the wsdl.

How to create XSD file programmatically in C#?

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);

Can intellisense be exported or extracted from Visual Studio to a text file?

I'm trying to write some documentation for a webservice that has been provided by one of our vendors for an application we're integrating. A bunch of the interface is custom objects defined in the web service itself. The vendor has put up significant resistance to providing any documentation for this application and so I've taken it upon myself to do their job for them [against my better judgement].
The documentation they have provided frankly is embarassing and I'm trying to make as short work of this as I possibly can to put some good quality docs together. I know that as I don't have access to their source, I can't just run it through nDoc/Sandcastle to spit out an API doc, but I was wondering if (as a half way house) there was an easy way to export the intellisense to a text file without me having to write a utility to specificially iterate through each of the object types defined and reflect the members out to text?
If I could do this, it would at least make sure that I have a good quality document structure where I can just fill in the blanks. Having to skip back and forth to Visual Studio to check the intellisense for every class member is a very laborious way of doing this.
Does anyone have any ideas?
If it is a web service that you are trying to document, couldnt you then parse out the WSDL?
If you are accessing a remote Web Service, then I think you have access to the corresponding WSDL: what about parsing it and look for just the information you need?
Or using a tool to do this (I Googled for "wsdl documentation generator")?
Or even using WSDL.exe to generate some dummy code from the WSDL and then document it, perhaps helped by GhostDoc?
HTH
Could you use Reflection to dump out the methods etc.?
Reflection is the feature in .Net, which enables us to get some information about object in runtime. That information contains data of the class. Also it can get the names of the methods that are inside the class and constructors of that object.
I think VS.net generates documentation for intellisense. For existing assemblies, it is already on your file system (e.g. C:\Windows\Microsoft.NET\Framework\v2.0.50727\en)
Try using the assembly from the vendor in VS.NET. Use process explorer or any such tool from sysinternals to see what files are being loaded. I am sure, you will find that there is an xml file created for the custom assembly (which is used to show the Intellisense and documentation available with it).
Hope that helps.
EDIT: I think the same folder (where your custom assemblies are located) will have the xml files for documentation.
If you have the dll's could you not decompile them and then recompile and use nDoc? That should give you a reasonalbe start.
Could you just use reflector (from redgate) to view the assembly (decompiled) instead of reproducing a API document. I'm not sure what else you would get of reflecting and building your on document that you wouldn't see live in reflector (of course this would depend on their writing readable code.
Maybe this is crazy, but could you take a screenshot of the full listing, and run it through an OCR program?

Categories

Resources