cdub
cdub

Reputation: 25731

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?

Upvotes: 5

Views: 10041

Answers (1)

MindingData
MindingData

Reputation: 12508

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

token["root"]["paramFile"].Count();

Upvotes: 9

Related Questions