←back to thread

294 points NotPractical | 2 comments | | HN request time: 0.384s | source
Show context
Spivak ◴[] No.41855620[source]
Any C# devs wanna explain the XML thing? To me having a separate class to deserialize each kind of XML document to its respective object seems nice and the "right" way to use XML. The class just becomes the config file. Generic loaders but poking at the tree is super brittle. Does C# come with magic to do this better?

Because if you have to invent the config file then isn't that creating a DSL and we're back to over engineering?

replies(3): >>41855737 #>>41855790 #>>41858899 #
1. jonathanlydall ◴[] No.41855790[source]
Active C# dev here, but I haven’t read the article.

For configuration these days XML is generally not used, they have a configuration system which can use a variety of underlying sources (like environment variables and JSON files) and you can either access these settings by key from a dictionary or trivially hydrate a plain old C# classes, including with collections.

People may still manually read their own configuration independent of this system or perhaps they’re just generally deserialising XML.

There are (I think) at least a few ways to work with XML in .NET.

For well known schemas I definitely generally recommend the C# class approach where you somewhat simply deserialize a file into well typed C# classes.

From your question it sounds like the XML API which allows you to arbitrarily query or manipulate XML directly was used here. I have on occasion used this when I don’t have the full schema for the XML available and need to tweak a single element or search for something with XQuery. It’s a useful tool for some scenarios, but a poor choice for others.

replies(1): >>41856592 #
2. Adachi91 ◴[] No.41856592[source]
System.Xml has been around for a very long time. I use it in one of my tools as a MSN deserialization class to make all my old MSN history "readable" by traversing the nodes and picking out sending/receiving user, timestamp, and message.