- (Answer to - if this question is a duplicate of how to deserialize XML to objects in C#)
My question is far more abstract than a concrete one, I didn't ask for a specific implementation, I wanted to hear approaches to find the best practice, should be relevant for any C# developer that seeks for the best C# code design when it comes to dealing with XML configuration files.
In JavaScript you can point to a JSON parent node & save it's reference. That reference holds only 1 descendants level, and those descendants carry the same reference to their descendants.
Example:
Config.Json (the file name)
"parentNode" : {
"descendant_1" :{
"content_1" : "abcde",
"content_2" : "abcdef"
},
"descendant_2" :{
"content_1" : "abcdefg",
"content_1" : "abcdefgh"
}
}
Now in JavaScript I can create a reference to that config.json plus to any node I want in it:
var data = Config.get('parentNode.descendant_2');
Get implementation:
Config.prototype.get = function get (key, defaultVal) {
try {
return JSON.parse(JSON.stringify(config.get(key)));
}catch (err) {
//return default value if one exists
if (typeof defaultVal !== 'undefined') return defaultVal; //replaces if(defaultVal) with if (arguments.length > 1) to avoid mistaking default value = false as null
//nothing to do, crash
throw err;
}
};
And now I'm able to pass as an argument to any function I want that config reference and inside any function I could keep digging into the nested nodes to reach which ever node I desire...
data.content_1
will give me abcdefg
data.content_2
will give me abcdefgh
My question is, what is the most efficient way to achieve such ability in C# using XML?
(Using XML and Strongly typed language isn't my call, I don't call those shots, so please avoid such irrelevant comments, thanks in advance)
XML file:
<role name="parentNode">
<descendant name="descendant_1">
<content_1 name="abcde"></content_1>
<content_2 name="abcdef"></content_2>
</descendant >
<descendant name="descendant_2">
<content_1 name="abcdefg"></content_3>
<content_2 name="abcdefgh"></content_4>
</descendant >
</role>
In C# I can call the parentNode in the following:
Linq to XML using XDocument
XPath
My goal is to find an approach that brings me as closer as possible to JavaScript's fluent api code writing.
Which means creating a reference to the parent node and gaining the ability to reach any of its descendants with a "fluent api".