Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
1.44/5 (2 votes)
See more:
I have to read multiple xml files from a folder and than stored the result in a txt file with filename as "nameoffileresult" . I have mentioned the problems in comments below.

var path = @"C:\Users\himansh.jain\Downloads\Xmltesting.xml";//here i have to read multiple file like these from the same folder
           var xml = File.ReadAllText(path);
           var codes = Regex.Matches(xml, @"P\d-\d-\d-\d-\d");
           List<string> list = new List<string>();
           foreach (var code in codes)
           {
               string s = Convert.ToString(code);
               list.Add(s);
           }
           List<string> uniqueList = list.Distinct().ToList();


           uniqueList.ForEach(i => Console.WriteLine($"{i}")); //here I have to store this result as nameofxmlfile_result.txt

           Console.ReadLine();


What I have tried:

example : name of xml files in folder
test.xml
test2.xml
test3.xml

result
test_result.txt
test2_result.txt
test3_result.txt
Posted
Updated 20-Jun-21 23:53pm

1 solution

Now you must take care while development and divide the task into logical chunks.

First of all you should develop a method that takes care of just getting the codes.
static IEnumerable<string> GetCodes(string filename)
{
	var xml = File.ReadAllText(filename);

	return Regex
		.Matches(xml, @"P\d-\d-\d-\d-\d")
		.Select(x => x.Value)
		.Distinct()
		.ToList();
}

The other function is called with the path of your XML files
static void AnalyzeFiles(string path)
{
	var filenames = Directory.GetFiles(path, "*.xml");

	foreach (var filename in filenames)
	{
		var codes = GetCodes(filename);

		var destinationFilename = Path.Combine(path, $"{Path.GetFileNameWithoutExtension(filename)}_result.txt");

		File.WriteAllLines(destinationFilename, codes);
	}
}

It can be called like this:
AnalyzeFiles(Path.GetDirectoryName(@"c:\temp\test"));

Now you should read the Microsoft documentation in order to understand things like "Path.Combine", "Path.GetFileNameWithoutExtension" etc.
 
Share this answer
 
Comments
Himansh jain 21-Jun-21 6:27am    
thank you for your solution , but now i am getting empty txt files. Value of ,
 Codes 
is not getting written in the text files.
Himansh jain 21-Jun-21 6:39am    
This code is running fine...the one you helped with before. Here I am getting values in my txt files.

using System;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;

namespace ReadXML
{
class Program
{
static void Main(string[] args)
{
var path = @"C:\Users\himansh.jain\Downloads\cesu";
foreach (var file in System.IO.Directory.GetFiles(path))
{
var xml = File.ReadAllText(file);
Console.WriteLine(file);
var codes = Regex.Matches(xml, @"P\d-\d-\d-\d-\d");
List<string> list = new List<string>();
foreach (var code in codes)
{
string s = Convert.ToString(code);
list.Add(s);
}
List<string> uniqueList = list.Distinct().ToList();

var destinationFilename = Path.Combine(path, $"{Path.GetFileNameWithoutExtension(file)}_result.txt");
File.WriteAllLines(destinationFilename, uniqueList);

}

Console.ReadLine();

}
}
}
TheRealSteveJudge 21-Jun-21 6:42am    
You once again did not follow my suggestions.
Please try the functions I have given you in my solution.
Himansh jain 21-Jun-21 6:54am    
Sir , i followed your suggestion only

using System;
using System.Xml;
using System.IO;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Linq;

namespace ReadXML
{
class Program
{
static void Main(string[] args)
{
AnalyzeFiles(Path.GetDirectoryName(@"C:\Users\himansh.jain\Downloads\cesu\"));

}

static IEnumerable<string> GetCodes(string filename)
{
var xml = File.ReadAllText(filename);

return Regex
.Matches(xml, @"P\d -\d -\d -\d -\d")
.Select(x => x.Value)
.Distinct()
.ToList();
}
static void AnalyzeFiles(string path)
{
var filenames = Directory.GetFiles(path, "*.xml");

foreach (var filename in filenames)
{
var codes = GetCodes(filename);

var destinationFilename = Path.Combine(path, $"{Path.GetFileNameWithoutExtension(filename)}_result.txt");

File.WriteAllLines(destinationFilename, codes);
}
}
}

}

But I am getting 0 byte size txt files therefore could not find those codes in them.
TheRealSteveJudge 21-Jun-21 7:05am    
Now your source code looks like mine.
Have a look at the regular expression.
Yours is @"P\d -\d -\d -\d -\d"
instead of @"P\d-\d-\d-\d-\d"
So the code attributes cannot be extracted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900