tags:

views:

236

answers:

3

Hi,

I have a XML file which holds configuration data for data sources, and the related queries held in 'dataseries' elements.

As I don't really need domain objects made up from the XML, rather just settings read and used to configure connections etc. I was wondering if there is any advantage in using the XML schema I have defined?

I am using LINQ to XML for reading my XML and initially thought it would be a good idea to use strongly typed XML.

Should I be using the .xsd or is it overkill?

A mock XML file:

 <?xml version="1.0" encoding="utf-8" ?>
<datasource name=" Datasource" cache="true">
  <database>
    <connection>
      <provider-name>sqlServer6.0</provider-name>
      <source name="E5"
            connectionString=""/>
    </connection>
    <update-interval>30</update-interval>
    <minimum-update-interval>2</minimum-update-interval>
  </database>
  <dataseries name="" identifier="e5">
    <graph-type></graph-type>
      <query>
        SELECT Period, Price
        FROM PriceUS
        WHERE Date = @date
      </query>
  </dataseries>
  <dataseries name="" identifier="e52">
    <graph-type></graph-type>
    <query>
      SELECT Period, Price
      FROM PriceUS
      WHERE Date = @date
    </query>
  </dataseries>
</datasource>
A: 

A schema is good when you want to validate the XML, for example, ensure that certain elements are present or have a limited set of values. A schema is also good if you will be doing a lot of work with the XML and it would be easier to convert the XML into C# objects -- in this case you can use the xsd.exe code generator to generate C# objects that can be marshaled and unmarshaled from the XML.

Eddie
+3  A: 

There are two levels of "correct" XML documents: well-formed and valid. Well-formed means it conforms to XML spec, and valid means that it conforms to your schema. If and when you are accepting an XML document from a total stranger, it's usually a good idea to check the validity of the document before moving forward.

As you mentioned, XML schema could also be used to generate XML databinding entities. When you publish a service or a schema to the world or your client, the schema document could be used as a spec. The world or your client can then use the XSD file to validate or databind to XML documents that you exchange.

eed3si9n
As the schema is part of a plugin framework the .xsd will have a benefit in being used as a spec. As I dont need to databind, and querys using LINQ will be as complex as things get I might just keep it simple!
theringostarrs
Do you have absolute control over the XML file? Could someone else modify it or send you a different one? If so, validate (schema). If not..probably validate anyway.
Andrew Coleson
A: 

Technically, there are two formal schema types in XML. There's the original schema syntax, called a Document Type Definition (DTD), which is a holdover from the ancient SGML days. And then there is the W3C standard, XSD, which is more compatible with modern data.

The only reason I mention this is that you might receive an XML file that is described using a DTD and you might need to know how to deal with it.

But please, friends don't let friends create DTDs for new applications.

Brian Travis
what about Relax NG Compact syntax?
eed3si9n