How to check if items of a list exist in another list in c#?

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 1449 Views - Last Post: 16 October 2017 - 10:09 AM Rate Topic: -----

#16 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to check if items of a list exist in another list in c#?

Posted 11 October 2017 - 05:31 AM

Also , just added to my last post, if you need to open the files and edit them the way you are saying, you could do this after the files have been obtained.

foreach (var item in xmlDupFiles)
            {
                //do what ever you need to do with file item's path from xml directory

                //obtain path of corrosponding file in meta directory
                var metaDirectoryFile = metaDupFiles.FirstOrDefault(s => Path.GetFileName(s) == Path.GetFileName(item));
                if (string.IsNullOrEmpty(metaDirectoryFile)) return;

                //do what ever you need to do with file item's path from meta directory

            }

Was This Post Helpful? 0
  • +
  • -

#17 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 16

Re: How to check if items of a list exist in another list in c#?

Posted 12 October 2017 - 07:20 AM

Hi JapanDave,
I am actually trying to replace the content of a node called label from the xml files residing in the xml folder by the content of the node lbl from the xml file which has the same name in the meta folder. So using your last posted code I've done the below
string basePath = textBox1.Text;
			List<string[]> xmlFolders = Directory.GetDirectories(basePath, "xml", SearchOption.AllDirectories).Select(item => Directory.GetFiles(item, "*.xml")).ToList();

			List<string[]> metaFolders = Directory.GetDirectories(basePath, "meta", SearchOption.AllDirectories).Select(item => Directory.GetFiles(item, "*.xml")).ToList();
			List<string> metaDupFiles = (from item in metaFolders.SelectMany(x => x)
			                             let sx = xmlFolders.SelectMany(s => s).Any(s => Path.GetFileName(s) == Path.GetFileName(item))
			                             where sx select item).ToList();
			List<string> xmlDupFiles = (from item in xmlFolders.SelectMany(x => x) let sx = metaFolders.SelectMany(s => s).Any(s => Path.GetFileName(s) == Path.GetFileName(item)) where sx select item).ToList();
			foreach (var item in xmlDupFiles)
			{
				
				var metaDirectoryFile = metaDupFiles.FirstOrDefault(s => Path.GetFileName(s) == Path.GetFileName(item));
				if (string.IsNullOrEmpty(metaDirectoryFile)) return;
				XDocument doc = Xdocument.Load(item);
				string name = doc.Root.Element("Emp").Element("lbl").Value;
				XDocument doc2 = Xdocument.Load(item);
				doc2.Root.Element("Employee").SetElementValue("label", name);
				doc2.Save(item);

			}
			MessageBox.Show("Process completed!");

But I'm kinda confused using the variable item to open two different files from two different folders and doing the above mentioned replace...
I'm new to all this, so I might have done some stupid mistakes in the code but I can't rectify it?
Can you help :helpsmilie:/>
Was This Post Helpful? 0
  • +
  • -

#18 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to check if items of a list exist in another list in c#?

Posted 12 October 2017 - 03:26 PM

You can't open two different files with the variable item as its file path is from the xml directory. That is what the metaDirrectoryFile variable is for. Change you last bit of code to this.

foreach (var item in xmlDupFiles)
{	
    var metaDirectoryFile = metaDupFiles.FirstOrDefault(s => Path.GetFileName(s) == Path.GetFileName(item));
    if (string.IsNullOrEmpty(metaDirectoryFile)) return;
    XDocument doc = Xdocument.Load(metaDirectoryFile);
    string name = doc.Root.Element("Emp").Element("lbl").Value;
    XDocument doc2 = Xdocument.Load(item);
    doc2.Root.Element("Employee").SetElementValue("label", name);
    doc2.Save(item);

}
MessageBox.Show("Process completed!");


If I am understanding your question correctly, the code should be changed to the above.
Was This Post Helpful? 1
  • +
  • -

#19 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 16

Re: How to check if items of a list exist in another list in c#?

Posted 13 October 2017 - 05:39 AM

Thanks JapanDave for your help. :bigsmile:/> Your last posted code worked :bananaman:/>
BTW, there are a few modifications that I'm trying to make to this code, like the xml files in xml folders are not properly structured so sometimes it becomes very tough to get the details of a node like how many parent nodes does it have and stuff like that which is required to use the root.Elements methods, so I was trying to do it using the Descendants method but facing some errors.
I've replaced the code
XDocument doc = Xdocument.Load(metaDirectoryFile);
                string name = doc.Root.Element("Emp").Element("lbl").Value;
                XDocument doc2 = Xdocument.Load(item);
                doc2.Root.Element("Employee").SetElementValue("label", name);
                doc2.Save(item);

by
XDocument doc = Xdocument.Load(metaDirectoryFile);
                var v = (from emp in doc.Descendants("lbl")
                            select emp).First().Value;
                XDocument doc2 = Xdocument.Load(item);
                doc2.Descendants("label").First().SetElementValue(v);
                doc2.Save(item);

Now, I'm getting an error
Error CS7036 There is no argument given that corresponds to the required formal parameter 'value' of 'XElement.SetElementValue(XName, object)
What am I doing wrong? :rolleyes2:/>
Was This Post Helpful? 0
  • +
  • -

#20 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5897
  • View blog
  • Posts: 20,136
  • Joined: 05-May 12

Re: How to check if items of a list exist in another list in c#?

Posted 13 October 2017 - 05:42 AM

The error is telling you exactly what the problem is. SetElementValue() is expecting to take 2 parameters. Your code seems to be passing only one.
Was This Post Helpful? 0
  • +
  • -

#21 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 16

Re: How to check if items of a list exist in another list in c#?

Posted 13 October 2017 - 06:03 AM

View PostSkydiver, on 13 October 2017 - 05:42 AM, said:

The error is telling you exactly what the problem is. SetElementValue() is expecting to take 2 parameters. Your code seems to be passing only one.

Well, how do I fix it?
Was This Post Helpful? 0
  • +
  • -

#22 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 16

Re: How to check if items of a list exist in another list in c#?

Posted 13 October 2017 - 06:14 AM

If I do
doc2.Descendants("label").First().SetElementValue("label",v);

Then I get the result like <label>5<label>100</label></label> where it should be <label>100</label>... :dontgetit:
Was This Post Helpful? 0
  • +
  • -

#23 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to check if items of a list exist in another list in c#?

Posted 13 October 2017 - 08:27 PM

I am sensing that a different approach for the first part of filtering is needed. As @Skydiver alluded in this post, first filtering the folders correctly would be the way to go.

Could you give an real life example of the folder structure to better get an idea as to how to tackle the filtering?
Was This Post Helpful? 0
  • +
  • -

#24 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 16

Re: How to check if items of a list exist in another list in c#?

Posted 13 October 2017 - 08:39 PM

View PostJapanDave, on 13 October 2017 - 08:27 PM, said:

Could you give an real life example of the folder structure to better get an idea as to how to tackle the filtering?

The folder structure is as follow:

D:\basePath\
            135-123-124\135\123\124\xml\135.xml
                        \meta\135.xml
            
            201-124-312\201\124\312\xml\201.xml
                        \meta\201.xml
and so on ...


BTW, what does it have to do with using the Descendants method :dontgetit:/>

This post has been edited by Whateva_: 13 October 2017 - 08:44 PM

Was This Post Helpful? 0
  • +
  • -

#25 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to check if items of a list exist in another list in c#?

Posted 13 October 2017 - 09:01 PM

I am not sure that you should be even doing this with the Descendants. Would it not be easier to deal with each xml and meta directory from each of the parent directories. At present, the code I made for you just got everything and threw it into a list.

ie, deal with parent directory 135-123-124 , get the files that match in the meta and xml directory and then move to the next parent, 201-124-312

Based on your example,in the parent 135-123-124 you have two files xml\135.xml and meta\135.xml, could this also be a possibility, xml\123.xml and meta\123.xml files existing?
Was This Post Helpful? 0
  • +
  • -

#26 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 16

Re: How to check if items of a list exist in another list in c#?

Posted 14 October 2017 - 08:29 AM

View PostJapanDave, on 13 October 2017 - 09:01 PM, said:

deal with parent directory 135-123-124 , get the files that match in the meta and xml directory and then move to the next parent, 201-124-312

Based on your example,in the parent 135-123-124 you have two files xml\135.xml and meta\135.xml, could this also be a possibility, xml\123.xml and meta\123.xml files existing?

I don't have any ideas as to how do I get each parent directory and get the files inside both meta and xml folders and do my replace, that is why I chose to get them in two different lists and match their file names and do my replace as each parent folders like 135-123-124 have only two xml files in total and they have the same name in the two folders meta and xml. Also the file names of other parent folders are different.

BTW, what is wrong using the Descendants method in my case, what other options do you propose?
Thanks in advance.

This post has been edited by Whateva_: 14 October 2017 - 08:30 AM

Was This Post Helpful? 0
  • +
  • -

#27 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5897
  • View blog
  • Posts: 20,136
  • Joined: 05-May 12

Re: How to check if items of a list exist in another list in c#?

Posted 14 October 2017 - 11:44 AM

Instead of letting the .NET library to do the recursion for you to gather the filenames, you'll need to recursively search for the files yourself. As much as it sounds like it'll be more work to do this yourself, it will actually simplify the problem for you because you'll have more control.

As for the Descendants issue within the XML itself, I suggest using an XPath query and use SelectSingleNode() or SelectNodes() to simplify targeting which element(s) you are interested in.
Was This Post Helpful? 0
  • +
  • -

#28 JapanDave  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 366
  • Joined: 01-February 16

Re: How to check if items of a list exist in another list in c#?

Posted 14 October 2017 - 05:27 PM

Will the filepath structure ALWAYS look like this,
D:\basePath\135-123-124\135\123\124\xml\135.xml\meta\135.xml

Or could it look like this as well,
D:\basePath\135-123-124\135\xml\135.xml\meta\135.xml\123\xml\123.xml\meta\123.xml\124\xml\124.xml\meta\124.xml


Just using the above as an overstated example, but what I am trying to get at is, do will you have multiple meta and xml in the parent directory or will only ever be one of each in each parent directory.
Was This Post Helpful? 0
  • +
  • -

#29 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 16

Re: How to check if items of a list exist in another list in c#?

Posted 14 October 2017 - 06:02 PM

View PostJapanDave, on 14 October 2017 - 05:27 PM, said:

Will the filepath structure ALWAYS look like this,
D:\basePath\135-123-124\135\123\124\xml\135.xml\meta\135.xml

Or could it look like this as well,
D:\basePath\135-123-124\135\xml\135.xml\meta\135.xml\123\xml\123.xml\meta\123.xml\124\xml\124.xml\meta\124.xml


Just using the above as an overstated example, but what I am trying to get at is, do will you have multiple meta and xml in the parent directory or will only ever be one of each in each parent directory.

Each parent directory will have only one meta and one xml directory and both the directories will have one xml each.
BTW, the structure of parent folders e.x. 135-123-124 are like folder 135-123-124 inside that there are two sub-folders 135 and meta, meta contains an xml 135.xml, and the folder 135 contains a sub-folder 123, that contains another sub-folder 124, that contains the sub-folder xml and then the xml folder contains 135.xml and some other *.tif files.

This post has been edited by Whateva_: 14 October 2017 - 06:04 PM

Was This Post Helpful? 0
  • +
  • -

#30 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5897
  • View blog
  • Posts: 20,136
  • Joined: 05-May 12

Re: How to check if items of a list exist in another list in c#?

Posted 14 October 2017 - 06:34 PM

And will the first 3 digits of the parent folder always match the names of the XML file? e.g. Given 135-123-124, you will be looking for ...\meta\135.xml and ...\xml\135.xml

And if that is true, is this also always true: 135-123-124 should let you find ...\135\123\124\xml\135.xml

Nevermind, my page just refreshed and it looks like you answered this question with our last section starting with BTW. This actually make the problem even easier to solve.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3