Asked by:
Parsing tnsnames.ora in c#

General discussion
-
Hi experts,
I am trying to parse tnsnames.ora file in c# to return the connections entries in the .ora file to list of collection in c#, I've tried the following code but it doesnot give me all the entries in the .ora file. Any help will be appreciated. Thanks
using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Text.RegularExpressions; namespace TNSNamesProcessing { public class TnsEntry { public string Name { get; set; } public string Host { get; set; } public string Port { get; set; } public string ServiceName { get; set; } } class TNSNamesReader { public string TNSNamesContents { get; set; } public TNSNamesReader() { } public TNSNamesReader(string tnsFile) { TNSNamesContents = File.ReadAllText(tnsFile); } public List<TnsEntry> Parse() { return Parse(TNSNamesContents); } public List<TnsEntry> Parse(string TNSNamesContents) { //string TNSPattren = @"([\w -] +)\s *= (?:\s |.) +?\)\s *\)\s *\)\s * ((?=[\w\-])|(?=$))"; string TNSPattren = @"[\n][\s]*[^\(][a-zA-Z0-9_.]+[\s]*=[\s]*\("; Regex rgxTNS = new Regex(TNSPattren, RegexOptions.Singleline | RegexOptions.IgnorePatternWhitespace); MatchCollection matches = rgxTNS.Matches(TNSNamesContents); var tnsEntries = new List<TnsEntry>(); foreach (Match match in matches) { var tnsEntry = new TnsEntry(); string matchedValue = match.Value.Trim(); tnsEntry.Name = Regex.Match(matchedValue, @" ^ ([^\s]+)", RegexOptions.IgnoreCase)?.Value.Trim(); tnsEntry.Host = Regex.Match(matchedValue, "(?<=HOST.+=) ([^)]*)", RegexOptions.IgnoreCase)?.Value.Trim(); tnsEntry.Port = Regex.Match(matchedValue, "(?<=PORT.+=) ([^)]*)", RegexOptions.IgnoreCase)?.Value.Trim(); tnsEntry.ServiceName = Regex.Match(matchedValue, "(?<=SERVICE_NAME.+=) ([^)]*)", RegexOptions.IgnoreCase)?.Value; tnsEntries.Add(tnsEntry); } return tnsEntries; } public string ReadTNSNamesFile(string tnsFile) { string inputString =""; /*collection to hold the names from the tnsnams file*/ List<string> tnsNamesCollection = new List<string>(); /*read the tnsnames file*/ try { FileInfo fi = new FileInfo(tnsFile); StreamReader sr = fi.OpenText(); inputString = sr.ReadToEnd(); //Console.WriteLine(inputString); sr.Close(); } catch (Exception e) { Console.WriteLine(e); } return inputString; }//end of method } }
- Changed type Seham_1981 Monday, November 16, 2020 9:25 AM Other
Saturday, November 14, 2020 8:58 AM
All replies
-
Show an example of your file that demonstrates the issues.
Saturday, November 14, 2020 9:44 AM -
Check a simplified sample approach:
string filecontents = @" MYOTHERSCHEMA = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = MYHOST)(PORT = 1234)) ) (CONNECT_DATA = (SERVICE_NAME = MYSERVICE.REMOTE) ) ) SOMEOTHERSCHEMA = (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP)(HOST = LOCALHOST)(PORT = 1234)) ) (CONNECT_DATA = (SERVICE_NAME = LOCAL) ) ) "; string pattern = @"(?imsx) ^(?<name>\S+)\s*= (?=.*?\(HOST\s*=\s*(?<host>\S+?)\)) (?=.*?\(PORT\s*=\s*(?<port>\S+?)\)) (?=.*?\(SERVICE_NAME\s*=\s*(?<service_name>\S+?)\)) "; foreach( Match m in Regex.Matches( filecontents, pattern ) ) { string name = m.Groups["name"].Value; string host = m.Groups["host"].Value; string port = m.Groups["port"].Value; string service_name = m.Groups["service_name"].Value; Console.WriteLine($"{name}, {host}, {port}, {service_name}"); }
Saturday, November 14, 2020 4:03 PM