maandag 10 november 2008

XML questions, serializing and numbering

Two questions I got today, regarding .Net, XML, XSD and XSLT:
1. I have an existing class that is Xml serializable. Can I make the serialization conform to an also existing XSD schema?
2. I have a sequence of elements in one XML. In transforming it to another XML (using XSLT), can I number the resulting sequence in te result?

One by one, that's the credo.

Question one is less of a problem than it seams: The differences are only in the ordering of elements. All the elements from the XSD are in fact present in the existing class. But can we steer the XML serializing so that it will use the ordering prescribed by the XSD schema?

There's a book I should be learning inside out, but I cannot find the time for it. It's annoying. But I knew it contained information about question 1. It the MCTS Application Development Foundation book. Well, chap 5, lesson 2, "How do I conform to an XML Schema". The solution is simple: use the Xml Schema Definition tool xsd.exe tool from the Visual Studio Command line:
xsd C:\schema\blabla.xsd /classes /language:CS

Of course, /language:VB is also a valid option.

Problem is: you generate a new class, but we already had an existing class.

Secondly, I went through the help for XmlSerializer and XmlWriterSettings. I didn't find any help there. And the attributes for steering the XmlSerializer don't seem very helpful either.

Conclusion: Do use the xsd tool, and manually adapt your existing class to match the generated class.

On to question 2. Simple. The answer is in the XSLT Cookbook, chapter 5.5, "Numbering textual output". Just use the xsl:number element, and read the help on it for several options.

Geen opmerkingen: