Created
March 16, 2020 19:44
-
-
Save masterholdy/75aeaf2e1da644c830270e12c63ada91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String abosulteFilePath = @"C:\Users\KNG HOLDY\Desktop\test.xml"; | |
String timePattern = "<Cube time=\"(\\d{4})-(\\d{2})-(\\d{2})\">"; //<Cube time="2020-03-16"> | |
String cubePattern = "<Cube currency=\"(.*?)\" rate=\"(.*?)\"/>"; // <Cube currency="JPY" rate="117.12"/> | |
Regex timeRegexp = new Regex(timePattern, RegexOptions.IgnoreCase); | |
Regex cubeRegexp = new Regex(cubePattern, RegexOptions.IgnoreCase); | |
using (var sr = new StreamReader(abosulteFilePath)) | |
{ | |
string xmlString = sr.ReadToEnd(); | |
//Trim XML-File | |
int firstCubeOccurnace = xmlString.IndexOf("<Cube>") + "<Cube>".Length; | |
int firstCubeEndOccurnace = xmlString.IndexOf("</Cube>"); | |
string firstCube = xmlString.Remove(firstCubeEndOccurnace).Remove(0, firstCubeOccurnace); | |
//Find Date of First Cube Occurance => <Cube time="2020-03-16"> | |
var timeMatch = timeRegexp.Match(firstCube); | |
if (timeMatch.Success) | |
{ | |
GroupCollection resultGroup = timeMatch.Groups; | |
if(resultGroup.Count == 4) | |
{ | |
String year = resultGroup[1].Value; | |
String month = resultGroup[2].Value; | |
String day = resultGroup[2].Value; | |
Console.WriteLine(String.Format("Date: {0}-{1}-{2}", year, month, day)); | |
} | |
} | |
else | |
{ | |
//TODO | |
Console.WriteLine("NO MATCH"); | |
} | |
//Find alle Cube's inside Parent Cube | |
foreach (Match cubeMatch in cubeRegexp.Matches(firstCube)) | |
{ | |
GroupCollection resultGroup = cubeMatch.Groups; | |
if(resultGroup.Count != 3) | |
{ | |
//TODO exception | |
} | |
String currency = resultGroup[1].Value; | |
String rate = resultGroup[2].Value; | |
Console.WriteLine(String.Format("Date: currency = {0}, rate = {1}", currency, rate)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment