views:

2178

answers:

6

We are utilizing the XML data type in Microsoft SQL Server 2005 for a project. Some members of the team and I feel that we should also use XSDs while members of the other camp feel that we should keep the XMLs ad hoc and not treat them as "types".

The XMLs are an effort to bring structure and centrality to a number of text configuration files that are a maintenance nightmare.

We are using .NET 3.5/C# and our tables are designed with the appropriate data types. My argument is that we are already "types oriented" in our thinking why break that approach because it is XML. It is because of the lack of types with the text files that the original problem occured. Not using a "types" approach leaves us open to the same problem.

May be my understanding of the benefits of XML schemas are incorrect. So what are the advantages and disadvantages of using XML schemas?

+1  A: 

Keeping a repository of XMLs without an XSD is akin (in my opinion) to having a database where all the types are declared as VARCHAR(n). You don't care what kind of input you get, you just want input.

XSDs assure that your XMLs have the type of input that you expect. They give structure to your model, the very thing you're looking for.

Swati
If you didnt say, I was going to. XML without Schema, is just plain text!
leppie
+2  A: 

Unfortunately even the authoring body of XSD (W3C) understands that XSD is a pretty bad technology. That said, it's intention isn't necessarily bad. One of C#'s major benefits is that it is statically typed. Statically typing your XML documents gives them the same benefits. What's probably best here is reverse engineering your classes to produce the Schema using the XML serialization attributes. When you do this C# will create a custom data reader for your XML file which will dramatically improve performance.

One of the biggest costs of XML is that it has to be string parsed. The more assumptions you can make about your XML files (e.g. their structure), the better your performance is likely to be.

So ultimately like many things, is their enough of a need for performance benefits to justify the costs in developer time. Or is there a strong enough desire to use statically typed systems to justify the cost of writing the XSD.

Ultimately your project needs will dictate what you should do, but static-typing and performance are major benefits to consider.

Orion Adrian
A: 

One big advantage to using schemas is that it helps make sure that everyone on the project agrees as to how an XML document should be laid out. Also, by using schemas, you can enable validation in your XML parser, making it easier to tell when some piece of code fails because it's given some bad XML.

On the downside, maintaining schemas can be a pain, and it may not be worth the effort depending on your project.

cgranade
+1  A: 

If you don't have a schema, you'll end up reimplementing all of the validation yourself (or not validating at all and crashing on invalid input). XSD parser/validators do all of that work for you, and are optimized and debugged by experts in their domain. Why would you redo all of that work yourself?

sk
+2  A: 

Well, as said in the other posts and in the question, XSD will ensure that you're using the right type at the right place in your XML and that you'll have to think twice before changing its structure.

But XSD is really over-verbose, if I can say so. And it's sometimes really a mess te describe a complex structure, with conditional content.

Hopefully, XSD is not the only way to validate a XML, a much more simpler approach is to use RelaxNG, and especially its compact syntax which is really more readable than what you could ever imagine with XSD.

gizmo
+1  A: 

XSD's are not the only XML schema available. Use http://relaxng.org/ instead. RelaxNG lets you express the schema in XML, rather than necessitating learning yet another data "language" as XSD's do.

David Arno