Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort files and move to their respectable directory (c#)

Tags:

split

xelement

So the issue I'm having is if a xml file contains "ONE" then it should move to the credit directory, else move to debit directory. Here is my current solution:

private void SplitAndMoveCreditCamts(FileInfo f)
{      
   if (v.Elements().Contains(x => x.Value == "ONE"))
      WriteToDirectory(v, "ONE\\");    
   else    
      WriteToDirectory(v, "TWO\\");
}

It does move the files to just the TWO directory. This means that the "WriteToDirectory" method is working, but ignores the "ONE" condition. I believe the error lies there.

like image 803
John Vicious Avatar asked Sep 18 '18 15:09

John Vicious


1 Answers

I think that your issue lies within the element line (v.Elements().Contains(x => x.Value == "ONE"))

The best solution for you is to make two seperate methods, ONE and TWO, where your SplitAndMoveONE is called.

private void SplitAndMoveONE(FileInfo f)
        {
            XElement[] els = GetXmlMessages(f.Name);

            foreach (var v in els)
            {
                XNamespace ns = v.Name.Namespace;
                bool exists = v.Descendants(ns + "ONE")
                 .Select(item => item.Value);

                if (exists)
                    WriteToDirectory(v, "ONE\\");
                else
                    // give error message
            }
        }

Namespace is vital in order to find the elements in your tag. The same applies to TWO.

like image 157
foyss Avatar answered Sep 29 '22 06:09

foyss