• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 578
  • Last Modified:

c# process xml file

I have the following xml file and need to loop through and process the js and css nodes in two steps. First I want to loop through the js, then the css.

<?xml version="1.0" encoding="utf-8" ?>

Open in new window

2 Solutions
Fernando SotoRetiredCommented:
Hi jackjohnson44;

The following Linq queries will return the need values from the XML data.

// Open the XML document from the path given
XDocument xdoc = XDocument.Load("Path & filename");

// Get the js values as a List of strings
List<string> jsResults = xdoc.Descendants("js")
                             .Select( js => js.Value ).ToList();

// Get the css values as a List of strings
List<string> cssResults = xdoc.Descendants("css")
                              .Select( css => css.Value ).ToList();

Open in new window

You then can use a for each loop to process the individual values of each list.
Carl TawnSystems and Integration DeveloperCommented:
Try something like:
            XmlDocument doc = new XmlDocument();
            doc.LoadXml("Your xml");

            foreach(XmlNode node in doc.SelectNodes("//js"))
                // do something

            foreach(XmlNode node in doc.SelectNodes("//css"))
                // do something

Open in new window

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now