7

I know this is a vague open ended question. I'm hoping to get some general direction.

I need to add cXML punchout to an ASP.NET C# site / application. This is replacing something that I wrote years ago in ColdFusion.

I'm a reasonably experienced C# developer but I haven't done much with XML. There seems to be lots of different options for processing XML in .NET.

Here's the open ended question: Assuming that I have an XML document in some form, eg a file or a string, what is the best way to read it into my code? I want to get the data and then query databases etc. The cXML document size and our traffic volumes are easily small enough so that loading the a cXML document into memory is not a problem.

Should I:

  1. Manually build classes based on the DTD and use the XML Serializer?

  2. Use a tool to generate classes. There are sample cXML files downloadable from Ariba.com. I tried xsd.exe to generate an XSD and then xsd.exe /c to generate classes. When I try to deserialize I get errors because there seems to be "confusion" around whether some elements should be single values or arrays.

I tried the CodeXS online tool but that gives errors in it's log and errors if I try to deserialize a sample document.

  1. Create a dataset and ReadXml()?

  2. Create a typed dataset and ReadXml()?

  3. Use Linq to XML. I often use Linq to Objects so I'm familiar with Linq in general but I'm struggling to see what it gives me in this situation.

  4. Some other means.

I guess I need to improve my understanding of XML in general but even so ... am I missing some obvious way of doing this? In the old ColdFusion site I found a free component ("tag") which basically ignored any schema and read the XML into a "structure" which is essentially a series of nested hash tables which was then easy to read in code. That was probably quite sloppy but it worked.

I also need to generate XML files from my C# objects. Maybe Linq to XML will be good for that. I could start with a default "template" document and manipulate it before saving.

Thanks for any pointers ...

Sandra Rossi
  • 11,934
  • 5
  • 22
  • 48
tetranz
  • 1,932
  • 3
  • 24
  • 32
  • Not a dupe persay, but have a look here: http://stackoverflow.com/questions/444116/what-are-the-best-functions-practices-and-or-techniques-to-read-write-xml-with – Kyle Rosendo May 21 '10 at 17:55

1 Answers1

1

If you need to generate arbitrary XML in an exact format, you should generate it manually using LINQ-to-XML.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964