I am trying to write an application among which one of the functions is to
determine the number of unique extensions found in a directory and all of
its sub directories. I am trying to use Linq to XML to do this.
Below is the code being used to accomplish what I am trying to do. In the
"if" statement, I am trying to update the <Count></CountElement to
"TESTING". However what I will really want is to add 1 to the existing
numerical value.
Your help is greatly appreciated!
System.IO.FileI nfo myCurrentFile;
System.Xml.Linq .XElement myFileCountCurr entElement;
var Extensions = new XElement("Count ", (from Extension in
_myFileExtensio nCount.LinqXDoc ument.Descendan ts("Extension" ) where
Extension.Eleme nt("Name").Valu e == myCurrentFile.E xtension.ToLowe r()
select new
{
Name =
Extension.Eleme nt("Name").Valu e,
Count =
Extension.Eleme nt("Count").Val ue,
}));
// An unaccounted file extension has been found. Let's add it.
if (Extensions.IsE mpty == true)
{
myFileCountCurr entElement = _myFileExtensio nCount.LinqXDoc ument.Root;
myFileCountCurr entElement =
_myFileExtensio nCount.AppendXm lElement(myFile CountCurrentEle ment,
"Extension" , null);
_myFileExtensio nCount.AppendXm lElement(myFile CountCurrentEle ment,
"Name", myCurrentFile.E xtension.ToLowe r());
_myFileExtensio nCount.AppendXm lElement(myFile CountCurrentEle ment,
"Count", "1");
}
else
{
Extensions.SetV alue("TESTING") ;
}
Below is the XML output that I am getting.
<?xml version="1.0" encoding="utf-8" ?>
- <FileExtensions >
- <Extension>
<Name>.m4b</Name>
<Count>1</Count>
</Extension>
- <Extension>
<Name>.docx</Name>
<Count>1</Count>
</Extension>
- <Extension>
<Name>.doc</Name>
<Count>1</Count>
</Extension>
- <Extension>
<Name>.xls</Name>
<Count>1</Count>
</Extension>
- <Extension>
<Name>.xlsx</Name>
<Count>1</Count>
</Extension>
</FileExtensions>