Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count the number of children in my JSON file using JSON.NET with LINQ

I have an xml with:

string xml = "<?xml .... />" +
    "<root>" +
        "<paramFile version=1.0>" +
            "<stuff />" +
        "</paramFile>" +
        "<paramFile version=1.0>" +
            "<stuff />" +
        "</paramFile>" +
     "</root>";

Then I convert to JSON and parse it:

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(xml);
    string jsonText = JsonConvert.SerializeXmlNode(doc).Replace("\"@", "\"");
    JToken token = JObject.Parse(jsonText);

How do I get the count of the number of paramFiles in my JSON?

like image 455
cdub Avatar asked Nov 21 '13 00:11

cdub


1 Answers

Something like this works (And I find is easiest). Mostly depends how deep your tree is going to be.

token["root"]["paramFile"].Count();
like image 107
MindingData Avatar answered Nov 08 '22 15:11

MindingData