how to write code to find element in directory in Linq - linq

How to search for each file in a single directory of xml files and get value of one element?
I tried to loop in Linq:
var files = Directory.GetFiles(C:\Users\valen\Downloads\2019-11-04 apmt, "*.xml", SearchOption.AllDirectories);
foreach (var file in files) {
var doc = XDocument.Load(file);
I need to find shipment element in <>.

It is hard to help when there is very little information about your XML given in OP. Assuming you are searching for Element ShipmentId in your xml files, you could search the Xml File using Linq. For example,
var listOfShipmentIds = new List<string>();
var files = Directory.GetFiles(folderToSearch, "*.xml", SearchOption.AllDirectories);
foreach(var file in files)
{
var dataRead = XDocument.Load(file)
.Root
.DescendantNodes()
.OfType<XElement>()
.Where(x=>x.Name.LocalName.Equals("ShipmentId"))
.Select(x=>x.Value);
listOfShipmentIds.AddRange(dataRead);
}
This would enable you to parse the Xml files and get the value of Element specified.

Related

Counting photos inside drawable folder [duplicate]

In my solution a had a folder with a few files. All this files have the Build Action "Embedded Resource".
With this code I can get a file:
assembly.GetManifestResourceStream(assembly.GetName().Name + ".Folder.File.txt");
But is there any way to get all *.txt files in this folder? A list of names or a method to iterate through them all?
You could check out
assembly.GetManifestResourceNames()
which returns an array of strings of all the resources contained. You could then filter that list to find all your *.txt files stored as embedded resources.
See MSDN docs for GetManifestResourceNames for details.
Try this, returns an array with all .txt files inside Folder directory.
private string[] GetAllTxt()
{
var executingAssembly = Assembly.GetExecutingAssembly();
string folderName = string.Format("{0}.Resources.Folder", executingAssembly.GetName().Name);
return executingAssembly
.GetManifestResourceNames()
.Where(r => r.StartsWith(folderName) && r.EndsWith(".txt"))
//.Select(r => r.Substring(folderName.Length + 1))
.ToArray();
}
NOTE: Uncomment the //.Select(... line in order to get the filename.
have a try with this. here you get all files
string[] embeddedResources = Assembly.GetAssembly(typeof(T)).GetManifestResourceNames();
T is of course your type. so you can use it generic
Just cracked this, use:
Assembly _assembly;
_assembly = Assembly.GetExecutingAssembly();
List<string> filenames = new List<string>();
filenames = _assembly.GetManifestResourceNames().ToList<string>();
List<string> txtFiles = new List<string>();
for (int i = 0; i < filenames.Count(); i++)
{
string[] items = filenames.ToArray();
if (items[i].ToString().EndsWith(".txt"))
{
txtFiles.Add(items[i].ToString());
}
}

Pick the latest file based on timestamp provided in the filename

I have to pick the files in order(first file first) from say a folder (C:\Users) and file name has the timestamp in it.
For example below are my files in C:\Users\ and the time stamp is after the first underscore i.e. 20170126102806 in the first file below. I have to loop through files and pick the first file and so on. so out of 5 files below,20170123-000011_20170126101823_AAA is the first file. How do I do this in SSIS?
1.20170123-000011_20170126102806_AAA
2.20170123-000011_20170126103251_AAA
3.20170123-000011_20170126101823_AAA
4.20170123-000011_20170126103305_AAA
5.20170123-000011_20170126102641_AAA
You can act in two ways:
use the foreach loop container to get the list of files, and then populate a database table.
Then, outside the foreach loop, use an Execute SQL to select from that table using an appropriate ORDER BY. Load an object variable with the result set. Then use a second foreach loop to step through the variable object and collect files.
use a Script Task to retrieve the contents of the folder (the list of files) and sort files then load an object variable with the dataset. Then use a foreach loop to step through the variable object to collect files.
I hope this help.
You could use a script task in a For Each Loop. Use the filename returned as the source to load each time.
using System.IO;
public void Main()
{
string filePath = "D:\\Temp";
DirectoryInfo dir = new DirectoryInfo(filePath);
var files = dir.GetFiles("*_AAA");//Or from a variable
DateTime fileCreateDate1 = File.GetCreationTime(filePath + "\\" + files[0]);
if (files.Length >= 2)
{
for (int i = 1; i < files.Length; i++)
{
DateTime fileCreateDate2 = File.GetCreationTime(filePath+ "\\" + files[i]);
if (fileCreateDate1 < fileCreateDate2)
{
fileCreateDate1 = fileCreateDate2;
}
}
}
Dts.Variables["User::FileToLoad"].Value = fileCreateDate1;
Dts.TaskResult = (int)ScriptResults.Success;
}
You will have to remove the file after it was loaded or else it will be loaded each time as it is the oldest or latest file.
There might be a bug or so, but have similar code that works. Just iron it out if needed.

alasql export to xlsx remove hashkey

I am using angularjs and javascript and want to export two arrays to Excel using alasql. The Excel file has two sheets, on every sheet there is one array.
In my Excel result I find an extra column $$hashkey.
According to the information I found, using angularjs, the $$hashkey is automatically removed. I also tried adding 'alasql.options.angularjs' but it did not help.
What am I doing wrong?
I am using the two arrays like this:
$scope.ExecutionsLC1: [[Execution,1,2,3],[Operators,1014,1019,1020],[Result,X,X,V]];
$scope.ExecutionsLC2: [[Execution,1,2,3],[Operators,2014,2019,2020],[Result,X,X,V]];
var opts = [{sheetid:'LC1',header:false},{sheetid:'LC2',header:false}];
var res = alasql('SELECT INTO XLSX("LCDetail.xlsx",?) FROM ?',[opts,[$scope.ExecutionsLC1,$scope.ExecutionsLC2]]);
it seems I can use angular.copy() to remove the $$hashkey.
var data1 = angular.copy($scope.ExecutionsLC1);
var data2 = angular.copy($scope.ExecutionsLC2);
var opts = [{sheetid:'One',header:false},{sheetid:'Two',header:false}];
var res = alasql('SELECT INTO XLSX("restest344b.xlsx",?) FROM ?',
[opts,[data1,data2]]);

Read data from txt files - using Linq-Entities?

in my current ASP.net MVC 3.0 project i am stuck with a situation.
I have four .txt files each has approximatly 100k rows of records
These files will be replaced with new files on weekly bases.
I need to Query data from these four text files, I am not able to choose the best and efficient way to do this.
3 ways I could think
Convert these text files to XML on a weekly basis and query it with Linq-XML
Run a batch import weekly from txt to SQL Server and query using Linq-Entities
avoid all conversions and query directly from text files.
Can any one suggest a best way to deal with this situation.
update:
Url of the Text File
I should connect to this file with credentials.
once i connect successfully, I will have the text file as below with Pipeline as Deliminator
This is the text file
Now i have to look up for the field highlighted in yellow and get the data in that row.
Note: First two lines of the text file are headers of the File.
Well As i Found a way my self. Hope this will be useful for any who are interested to get this done.
string url = "https://myurl.com/folder/file.txt";
WebClient request = new WebClient();
request.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["UserName"], ConfigurationManager.AppSettings["Password"]);
Stream s = request.OpenRead(url);
using (StreamReader strReader = new StreamReader(s))
{
for (int i = 0; i <= 1; i++)
strReader.ReadLine();
while (!strReader.EndOfStream)
{
var CurrentLine = strReader.ReadLine();
var count = CurrentLine.Split('|').Count();
if (count > 3 && CurrentLine.Split('|')[3].Equals("SearchString"))
{
#region Bind Data to Model
//var Line = CurrentLine.Split('|');
//CID.RecordType = Line[0];
//CID.ChangeIdentifier = Line[1];
//CID.CoverageID = Convert.ToInt32(Line[2]);
//CID.NationalDrugCode = Line[3];
//CID.DrugQualifier = Convert.ToInt32(Line[4]);
#endregion
break;
}
}
s.Close();
}
request.Dispose();

HTMLAgilityPack ChildNodes index works, named node does not

I am parsing an XML API response with HTMLAgilityPack. I am able to select the result items from the API call.
Then I loop through the items and want to write the ChildNodes to a table. When I select
ChildNodes by saying something like:
sItemId = dnItem.ChildNodes(0).innertext
I get the proper itemId result. But when I try:
sItemId = dnItem.ChildNodes("itemId").innertext
I get "Referenced object has a value of 'Nothing'."
I have tried "itemID[1]", "/itemId[1]" and a veriety of strings. I have tried SelectSingleNode and ChildNodes.Item("itemId").innertext. The only one that has worked is using the index.
The problem with using the index is that sometimes child elements are omitted in the results and that throw off the index.
Anybody know what I am doing wrong?
HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlDocument();
htmlDoc.LoadHtml(#webHTMLeditor.TextXhtml);
HtmlNodeCollection tableRows = htmlDoc.DocumentNode.SelectNodes("//tr");
for (int i = 0; i < tableRows.Count; i++)
{
HtmlNode tr = tableRows[i];
HtmlNode[] td = new HtmlNode[2];
string xpath = tr.XPath + "//td";
HtmlNodeCollection cellRows = tr.SelectNodes(#xpath);
//td[0] = tr.ChildNodes[1];
//td[1] = tr.ChildNodes[3];
try
{
td[0] = cellRows[0];
td[1] = cellRows[1];
}
catch (Exception)
{ }
//etc
}
The code is used to extract data from a table, row by row, by cell per row.
I used the existing xpath and I altered it acording to my needs.
Good luck!

Resources