using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Xml.Linq;
namespace myApp
{
class Program
{
private static readonly Decimal ratio = Decimal.Parse("6.5", new CultureInfo("en-US"));
private const string SourceDirectory = @"D:\SVN\C2C\";
private const string OutputOriginalDirectory = @"D:\LLOrig\";
private const string OutputRescaledDirectory = @"D:\LLResc\";
static void Main(string[] args)
{
string startFolder = SourceDirectory + @"Assets\";
DirectoryInfo dir = new DirectoryInfo(startFolder);
IEnumerable<FileInfo> fileList = dir.GetFiles("*.*", SearchOption.AllDirectories);
Regex regex = new Regex("(iMinLatitude)|(iMaxLatitude)");
var queryMatchingFiles =
from file in fileList
where file.Extension == ".xml"
let fileText = GetFileText(file.FullName)
where regex.IsMatch(fileText)
select file.FullName;
Console.WriteLine("The text was found in the following files:");
foreach (string filename in queryMatchingFiles)
{
Console.WriteLine(filename);
string newFilename = filename.Replace(SourceDirectory, OutputOriginalDirectory);
XDocument doc = XDocument.Parse(GetFileText(filename), LoadOptions.PreserveWhitespace);
Directory.CreateDirectory(Path.GetDirectoryName(newFilename));
doc.Save(newFilename);
XElement root = doc.Root;
IEnumerable<XElement> rootDescs = root.Descendants();
IEnumerable<XElement> allMins =
from desc in rootDescs
where desc.Name.LocalName == "iMinLatitude"
select desc;
List<XElement> allMinList = allMins.ToList();
foreach (XElement minElement in allMins)
{
var reduced = ReduceValue(minElement.Value);
minElement.SetValue(reduced);
}
IEnumerable<XElement> allMaxs =
from desc in rootDescs
where desc.Name.LocalName == "iMaxLatitude"
select desc;
List<XElement> allMaxList = allMaxs.ToList();
foreach (XElement maxElement in allMaxs)
{
var reduced = ReduceValue(maxElement.Value);
maxElement.SetValue(reduced);
}
string rescaledFilename = filename.Replace(SourceDirectory, OutputRescaledDirectory);
Directory.CreateDirectory(Path.GetDirectoryName(rescaledFilename));
doc.Save(rescaledFilename);
}
}
// Read the contents of the file.
// Source:
https://docs.microsoft.com/en-us/do...o-query-the-contents-of-files-in-a-folder-lin
static string GetFileText(string name)
{
string fileContents = String.Empty;
// If the file has been deleted since we took
// the snapshot, ignore it and return the empty string.
if (File.Exists(name))
{
fileContents = File.ReadAllText(name);
}
return fileContents;
}
static string ReduceValue(string original)
{
return Decimal.ToInt32(Decimal.Round(Convert.ToDecimal(original) / ratio)).ToString();
}
}
}