C# XML XSD Objects

Quite often our app needs to import/export XML.

Of course you can use Microsoft’s LINQ to XML to query/manipulate XML, essentially a object model, lighter weight than DOM.

However, we may prefer to work with typed an entity object and we can

  • read an XML and deserialize into an object. 
  • serialize our entity object to xml

There are many ways to achieve typed XML programming.  I will list of a couple of free approaches:

  1. xsd.exe.  This has been around for a long time and it was built by Microsoft.  It uses array.  It is .NET 4.0 compatible.  It is part of Microsoft Windows SDKs.  For example, C:\Program Files\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4.0 Tools.
  2. Xsd2Code is a Codeplex project.  Version 3.6 supports Visual Studio 2012 Add-in support.  It can use generic collections (i.e. List<ABC>)
  3. LinqToXsd is another Codeplex project.  This creates .NET classes with better fidelity than xsd.exe. 

The tools above require an XML Schema (*.xsd) file as an input in order to generate .NET classes.  Ways to generate XML Schema include:

  • use Visual Studio XML Editor’s menu “XML — > Create Schema”
  • use xsd.exe

Note that they only infer (i.e. guess) the XML Schema based on an existing XML file.  So choose a bigger/more complex XML file if you can.  It would be best if you can get a XML schema file from the maker of the XML file.

For me, I like LinqToXsd and Xsd2Code.  I only use Xsd.exe as a fall backup.

References:

Advertisements

CS1647: An expression is too long or complex to compile

I ran into this ASP.NET error. 

After a long process of trials and errors to determine the affected HTML codes area, then I insert a simple comment.  I guess the insertion of a comment break some “expression”, making it easier to compile.

<%
   1: -- Just a comment--

%>

This post is more for reference since I am neither sure of the cause nor the workaround.

Troubleshoot jQuery ajax() CORS

It can be challenging to troubleshoot jQuery ajax() failures. 

Sometimes they are due to Cross-Domain policy, or Cross Origin Resource Sharing (CORS). 

Other times, you are misled.  For example, IE10 console messages may say:

“XMLHttpRequest for http://xxxx.com/xxxx requires Cross Origin Resource Sharing (CORS).”

But the real cause would be just an authentication error.

Tips:

  1. Use Fiddler to see what the actual server response error is.
  2. Use Fiddler Composer to make direct ajax calls to the api server, bypassing your own web server.  If it does not work, then it is probably has nothing to do work CORS.
  3. If it is a real CORS issue, know what your ajax call is categorized as: simple requests or preflight requests.
  4. If you see your browser makes a OPTION request, then it is doing a preflight request.  Nowadays, it is probably due to the fact that you are using custom headers in the call.
  5. Following the articles below to fix it.  Basically, it is just adding the correct HTTP response headers.  You can do it at the code level.  Or you can do it at your web server.  For IIS, you can use web.config or IIS Http Response Headers.  The headers to add include:
    • Access-Control-Allow-Origin – necessary for any CORS.  It can be set to “*” or your domain hosting the webpage “http://xxxx.com”.
    • Access-Control-Allow-Methods – necessary if you use HTTP methods other than GET, POST, or HEAD.
    • Access-Control-Allow-Headers – necessary if you use custom headers when you make the ajax call.
    • Access-Control-Allow-Credentials – necessary if you use credentials (i.e. cookies or HTTP Auth).

Tools:

Fiddler, Browser Console

Reference:

http://techblog.constantcontact.com/software-development/using-cors-for-cross-domain-ajax-requests/

https://developer.mozilla.org/en-US/docs/HTTP/Access_control_CORS