使用循环在XDocument中动态创建XElements

时间:2015-08-21 09:39:12

标签: c# html foreach linq-to-xml xelement

我需要能够使用循环在XDocument中动态创建'n'个XElements,具体取决于目录中找到的文件数。

我的WIP骨干项目代码可能需要稍长才能粘贴到此处,所以我已将其添加到pastie.org - here

你会看到第73-91行之间我有一个foreach循环搜索目录&它是子目录,并定位其中包含的所有文件的路径。 我知道这就像我用过的那样:

int x = filePaths.Length;

并且x的输出与给定目录中的文件数匹配。

代码的主要部分也可以工作并根据需要创建HTML文件,但我还需要它在我放置的第215行之间动态创建'n'XElements:

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

在打开<TR>代码之前

第280行,在关闭<TR>标记之后,因此在指定目录中找到的每个文件都有自己的<TABLE>行。

通过在整个代码中移动循环但仍然遇到大量错误的砖墙,我已经不知道我尝试实现这一目标的方式数量。

最初我认为只需将循环放在第215行的代码中即可,但这样做会导致许多错误,例如丢失:

  • }

等...

现在我完全卡住了。

我试图在C#中模仿我前一段时间制作的批处理文件,该文件使用的WMIC工作得非常好,但是有限的&amp;我希望增加一些功能,这就是C#的用武之地。

批处理文件使用循环,就像我在这里尝试一样,没有任何问题。

以下是WMIC批处理文件中的循环:

(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))))

我发现Create XML with XDocument with a variable number of XElements using for loop将搜索&amp;最初是有希望的,虽然我确实根据文件的数量动态创建了'n'个XElements,变量DateTaken只显示了目录中最后一个文件的值,所以我有一个{{ 1}}有68行<TABLE>,对于我正在使用的测试目录,它们都包含从循环找到的最终文件中检索的值。

非常感谢任何帮助。

问候...,

2 个答案:

答案 0 :(得分:1)

不要使用循环,使用LINQ from .. select ..表达式,例如

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))
  ))

要提取日期,请编写一个封装代码的方法,例如

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";
                                        }
}

答案 1 :(得分:0)

如果您正在使用XElements,那么它们可以与Linq一起使用,请尝试以下方法:

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