Dynamically creating XElements inside XDocument using a loop

I need to be able to dynamically create 'n' number of XElements inside an XDocument using a loop and depending on the number of files found within a directory.

My WIP bare bones project code is possibly slightly long to paste here so I have added it to pastie.org - here

You will see that between lines 73-91 that I have a foreach loop that searches through a directory & it's sub-directories and locates the paths of all the files contained within. I know that this on it's own works as I have used:

int x = filePaths.Length;

and the output of x matches the number of files in the given directory.

The main bulk of the code also works and creates a HTML file as I require but I also need it to dynamically create 'n' XElements between line 215 where I have placed:

/// **** NEED TO INSERT LOOP HERE TO DYNAMICALLY CREATE NUMBER OF ELEMENTS DEPENDING ON NUMBER OF FILES IN DIRECTORY ****

just before the opening of a <TR> tag & line 280, just after the closing of a <TR> tag so each file found within the specified directory will have it's own <TABLE> row.

I have lost count as to the number of ways I have tried to achieve this by moving the loop around throughout the code but keep hitting a brick wall with masses of errors.

Initially I thought it would be as simple as to just place the loop in the code at line 215 but doing this causes numerous errors such as missing:

  • ;
  • }

etc...

Now I am completely stuck.

I am trying to emulate in C# a batch file I made some time ago that uses WMIC that works perfectly well but is limited & I wish to add a few more features, this is where C# comes in.

The batch file uses a loop, just as I am trying to here, without any problems at all.

Here is the loop from the WMIC batch file:

(FOR /f "delims=" %%a IN ('dir /b /a-d /s /n "C:\Users\1CM69\Pics & Vids\Archives\Family\2002"') DO (
FOR /f "tokens=1-3*" %%x IN ('dir /a-d /tc "%%~a"^|findstr "^[0-9]"') DO (
  for /f %%c in ("%%~ta") do (
ECHO ^<TR^>^<td style="border-width: 1px;padding: 10px;border-style: inset;border-color: gray;background-color: white;" align="center" valign="middle"^>^<a href="%%a" target="_new"^>^<img src="%%a" width="100px" border="0"^>^</a^>^</TD^>^<td style="border-width: 1px;padding: 10px;border-style: inset;border-color: gray;background-color: white;" align="left" valign="middle"^>^<B^>%%~nxa^</B^>^</TD^>^<td style="border-width: 1px;padding: 10px;border-style: inset;border-color: gray;background-color: white;" align="center" valign="middle"^>^<B^>%%~c^</B^>^</TD^>^<td style="border-width: 1px;padding: 10px;border-style: inset;border-color: gray;background-color: white;" align="center" valign="middle"^>^<B^>%%x^</B^>^</TD^>^</TR^> >> 2002.html))))

I found Create XML with XDocument with a variable number of XElements using for loop will searching around & was initially hopeful and although I did get it to dynamically create 'n' number of XElements based on the number of files, the variable DateTaken only showed the value of the last file within the directory, so I had a <TABLE> with 68 rows <TR>, in the case of the test directory I am using, and they all contained the values retrieved from the final file that the loop found.

Any help would be kindly appreciated.

Regards..,

Answers


Don't use a loop, use a LINQ from .. select .. expression e.g.

from file in filePaths
select new XElement("tr", 
  new XElement("td",
    new XElement("a", new XAttribute("href", file), new XAttribute("target", "_new"), new XElement("img", new XAttribute("src", file), new XAttribute("width", "100"))),
    new XElement("td", file),
    new XElement("td", GetDate(file))
  ))

To extract the date write a method that encapsulates your code e.g.

string GetDate(string path) {

try
                                        {

                                        Bitmap MyPhoto = new Bitmap(file);
                                        const int IDDateTimeOriginal = 36867;
                                        PropertyItem TakenDate = MyPhoto.GetPropertyItem(IDDateTimeOriginal);
                                        Encoding ascii = Encoding.ASCII;
                                        return ascii.GetString(TakenDate.Value, 0, TakenDate.Len - 1);
                                        }


                                        catch //(ArgumentException) if the property doesn't exists
                                        {
                                          return "MISSING ENTRY";
                                        }
}

If you are using XElements they are designed to work with Linq so try something like this:

var filePaths = GetFiles(@"C:\SomeDir");
var xml = new XElement("tr", 
    filePaths.Select(fp => new XElement("td", fp.FullName)));

Need Your Help

Calculating camera ray direction to 3d world pixel

graphics camera pixel tracing ray

I'm want to calculate ray directions from camera to pixel positions (in world coords) with given height as described in this paper. Camera image size is 640,480. I calibrated the intrinsic camera

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.