0
  • (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".

USS-Montana
  • 397
  • 4
  • 15
  • Possible duplicate of [Deserializing XML to Objects in C#](http://stackoverflow.com/questions/226599/deserializing-xml-to-objects-in-c-sharp) – Khalil Khalaf Aug 11 '16 at 12:54
  • 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. – USS-Montana Aug 11 '16 at 13:22
  • It is still a **possible** duplicate. – Khalil Khalaf Aug 11 '16 at 13:26
  • I believe it isn't possible. – USS-Montana Aug 11 '16 at 13:28

2 Answers2

0

One can use Linq to Xml + the XPathSelectElements extension method: (add using System.Xml.XPath;)

var items = XDocument.Load("data.xml")
                     .XPathSelectElements("/role[@name='parentNode']/*[@name='descendant_2']/*");

You can also just with linq to xml:

var items = XDocument.Load("data.xml")
                     .Descendants("descendant")
                     .Where(element => element.Attribute("name").Value == "descendant_2")
                     .Descendants();
Gilad Green
  • 36,708
  • 7
  • 61
  • 95
  • Yes Gilad, I'm well aware of those 2 approaches, I'm looking for new ones, to be able to make a better decision – USS-Montana Aug 11 '16 at 13:26
0

If your XML config has a static format, you should look into xml serialization, This will allow you to convert the XML into a C# Object.

You could also use dynamic, but it I wouldn't recommend it, unless, well your xml format is dynamic.

gilmishal
  • 1,884
  • 1
  • 22
  • 37