EDN Admin
Well-known member
I am relatively new to LINQ and have traditionally been using the XmlDocument object to parse through XML Document structures. I have been reading a little bit about LINQ to XML and LINQ to Collections, but have not exactly seen how to refactor my
existing code base to make it cleaner.
This is some code that I wrote earlier to parse the Code Metrics output from the Visual Studio 2010 Code Metrics Power Tool:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Xml;
<span style="color:Blue; using System.Configuration;
<span style="color:Blue; using System.IO;
<span style="color:Blue; namespace ProcessCodeMetrics
{
<span style="color:Blue; class Program
{
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
ProcessMetricsFile();
Console.ReadKey();
}
<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; void ProcessMetricsFile()
{
XmlDocument xmlDom = <span style="color:Blue; new XmlDocument();
xmlDom.Load(ConfigurationManager.AppSettings[<span style="color:#A31515; "MetricsFilePath"]);
XmlElement rootElement = xmlDom.DocumentElement;
XmlNodeList modules = rootElement.GetElementsByTagName(<span style="color:#A31515; "Module");
<span style="color:Blue; for (<span style="color:Blue; int i=0; i < modules.Count; i++)
{
XmlElement module = modules.Item(i) <span style="color:Blue; as XmlElement;
DisplayNodeName(module);
<span style="color:Green; //Console.WriteLine(string.Format("The name of the module is: {0}", module.GetAttribute("Name")));
<span style="color:Green; //Console.WriteLine(string.Format("The name of the first child element is: {0}", module.FirstChild.Name));
<span style="color:Green; //Get Metrics for the module
GetMetrics(module.FirstChild.ChildNodes);
XmlNodeList namespaces = module.GetElementsByTagName(<span style="color:#A31515; "Namespace");
<span style="color:Green; //Iterate through Namespaces
<span style="color:Blue; for (<span style="color:Blue; int j = 0; j < namespaces.Count; j++)
{
XmlElement metricNamespace = namespaces.Item(j) <span style="color:Blue; as XmlElement;
DisplayNodeName(metricNamespace);
GetMetrics(metricNamespace.FirstChild.ChildNodes);
XmlNodeList types = metricNamespace.GetElementsByTagName(<span style="color:#A31515; "Type");
<span style="color:Green; //Iterate through Types
<span style="color:Blue; for (<span style="color:Blue; int k = 0; k < types.Count; k++)
{
XmlElement metricType = types.Item(k) <span style="color:Blue; as XmlElement;
DisplayNodeName(metricType);
GetMetrics(metricType.FirstChild.ChildNodes);
XmlNodeList members = metricType.GetElementsByTagName(<span style="color:#A31515; "Member");
<span style="color:Green; //Iterate through Members
<span style="color:Blue; for (<span style="color:Blue; int m = 0; m < members.Count; m++)
{
XmlElement memberMetric = members.Item(m) <span style="color:Blue; as XmlElement;
DisplayNodeName(memberMetric);
GetMetrics(memberMetric.FirstChild.ChildNodes);
}<span style="color:Green; //for
}<span style="color:Green; //for
}<span style="color:Green; //for
}
}
<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; void DisplayNodeName(XmlElement xmlNode)
{
<span style="color:Blue; string nodeName = xmlNode.GetAttribute(<span style="color:#A31515; "Name");
File.AppendAllText(ConfigurationManager.AppSettings[<span style="color:#A31515; "MetricsOutputFilePath"], nodeName);
}
<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; void GetMetrics(XmlNodeList metrics)
{
StringBuilder strMetricText = <span style="color:Blue; new StringBuilder();
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < metrics.Count; i++)
{
XmlElement metric = metrics.Item(i) <span style="color:Blue; as XmlElement;
<span style="color:Blue; string strMetricInfo = <span style="color:Blue; string.Format(<span style="color:#A31515; "{0}={1}", metric.GetAttribute(<span style="color:#A31515; "Name"), metric.GetAttribute(<span style="color:#A31515; "Value"));
strMetricText.AppendLine(strMetricInfo);
<span style="color:Green; //Console.WriteLine(metric.GetAttribute("Name"));
<span style="color:Green; //Console.WriteLine(metric.GetAttribute("Value"));
}
File.AppendAllText(ConfigurationManager.AppSettings[<span style="color:#A31515; "MetricsOutputFilePath"], strMetricText.ToString());
}
}
}
[/code]
<div style="color:Black;background-color:White; <pre>
Any sample code or snippets <span style="color:Blue; on how to refactor and clean up the code to utilize LINQ and reduce the number of looping iterations would be greatly appreciated.
[/code]
<div style="color:Black;background-color:White; <pre>
Thanks.
[/code]
View the full article
existing code base to make it cleaner.
This is some code that I wrote earlier to parse the Code Metrics output from the Visual Studio 2010 Code Metrics Power Tool:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Xml;
<span style="color:Blue; using System.Configuration;
<span style="color:Blue; using System.IO;
<span style="color:Blue; namespace ProcessCodeMetrics
{
<span style="color:Blue; class Program
{
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
ProcessMetricsFile();
Console.ReadKey();
}
<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; void ProcessMetricsFile()
{
XmlDocument xmlDom = <span style="color:Blue; new XmlDocument();
xmlDom.Load(ConfigurationManager.AppSettings[<span style="color:#A31515; "MetricsFilePath"]);
XmlElement rootElement = xmlDom.DocumentElement;
XmlNodeList modules = rootElement.GetElementsByTagName(<span style="color:#A31515; "Module");
<span style="color:Blue; for (<span style="color:Blue; int i=0; i < modules.Count; i++)
{
XmlElement module = modules.Item(i) <span style="color:Blue; as XmlElement;
DisplayNodeName(module);
<span style="color:Green; //Console.WriteLine(string.Format("The name of the module is: {0}", module.GetAttribute("Name")));
<span style="color:Green; //Console.WriteLine(string.Format("The name of the first child element is: {0}", module.FirstChild.Name));
<span style="color:Green; //Get Metrics for the module
GetMetrics(module.FirstChild.ChildNodes);
XmlNodeList namespaces = module.GetElementsByTagName(<span style="color:#A31515; "Namespace");
<span style="color:Green; //Iterate through Namespaces
<span style="color:Blue; for (<span style="color:Blue; int j = 0; j < namespaces.Count; j++)
{
XmlElement metricNamespace = namespaces.Item(j) <span style="color:Blue; as XmlElement;
DisplayNodeName(metricNamespace);
GetMetrics(metricNamespace.FirstChild.ChildNodes);
XmlNodeList types = metricNamespace.GetElementsByTagName(<span style="color:#A31515; "Type");
<span style="color:Green; //Iterate through Types
<span style="color:Blue; for (<span style="color:Blue; int k = 0; k < types.Count; k++)
{
XmlElement metricType = types.Item(k) <span style="color:Blue; as XmlElement;
DisplayNodeName(metricType);
GetMetrics(metricType.FirstChild.ChildNodes);
XmlNodeList members = metricType.GetElementsByTagName(<span style="color:#A31515; "Member");
<span style="color:Green; //Iterate through Members
<span style="color:Blue; for (<span style="color:Blue; int m = 0; m < members.Count; m++)
{
XmlElement memberMetric = members.Item(m) <span style="color:Blue; as XmlElement;
DisplayNodeName(memberMetric);
GetMetrics(memberMetric.FirstChild.ChildNodes);
}<span style="color:Green; //for
}<span style="color:Green; //for
}<span style="color:Green; //for
}
}
<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; void DisplayNodeName(XmlElement xmlNode)
{
<span style="color:Blue; string nodeName = xmlNode.GetAttribute(<span style="color:#A31515; "Name");
File.AppendAllText(ConfigurationManager.AppSettings[<span style="color:#A31515; "MetricsOutputFilePath"], nodeName);
}
<span style="color:Blue; private <span style="color:Blue; static <span style="color:Blue; void GetMetrics(XmlNodeList metrics)
{
StringBuilder strMetricText = <span style="color:Blue; new StringBuilder();
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < metrics.Count; i++)
{
XmlElement metric = metrics.Item(i) <span style="color:Blue; as XmlElement;
<span style="color:Blue; string strMetricInfo = <span style="color:Blue; string.Format(<span style="color:#A31515; "{0}={1}", metric.GetAttribute(<span style="color:#A31515; "Name"), metric.GetAttribute(<span style="color:#A31515; "Value"));
strMetricText.AppendLine(strMetricInfo);
<span style="color:Green; //Console.WriteLine(metric.GetAttribute("Name"));
<span style="color:Green; //Console.WriteLine(metric.GetAttribute("Value"));
}
File.AppendAllText(ConfigurationManager.AppSettings[<span style="color:#A31515; "MetricsOutputFilePath"], strMetricText.ToString());
}
}
}
[/code]
<div style="color:Black;background-color:White; <pre>
Any sample code or snippets <span style="color:Blue; on how to refactor and clean up the code to utilize LINQ and reduce the number of looping iterations would be greatly appreciated.
[/code]
<div style="color:Black;background-color:White; <pre>
Thanks.
[/code]
View the full article