I am writing a WebService that is supposed to convert DataSet into XML. My DataSet is actually list of hotels with categories. Each hotel can belong to one or more categories. I
Is one structure better than the other one?
Better for what? The DataSet
's format is very good if you want to be able to read the data back into a DataSet
, so by that standard, at least, it's superior to the one you're proposing.
Generally speaking, when you process an XML document, you will be searching it for the elements that you want to work with using XPath or Linq. Compare:
var categories = hotelElement.SelectNodes("category");
var categories = hotelXElement.Elements("category");
with:
var categories = hotelElement.SelectNodes("categories/category");
var categories = hotelXElement.Elements("categories").Element("category");
What does having intermediate categories
elements get you? The XML looks a little bit nicer in an editor. That's generally not a compelling advantage, especially if it makes the XML a little harder to process.
Is it possible to make relation between two tables in DataSet so I get output like it is in second (desired) XML example.
No. The serialization format of the DataSet
is very rigidly defined. It supports a couple of options (nesting, including the schema, diffgrams), but the format doesn't change (unless you're saving as diffgrams, but if you do that, how the XML looks is very low on your list of needs).
You can use XSLT to change the format pretty readily, though. Add this to the identity transform: