我正在使用以下代码遍历目录,查找 xml 文件并将它们读入:
XmlReader reader = null;
foreach (string file in files)
{
try
{
System.IO.FileInfo fi = new System.IO.FileInfo(file);
string fext = fi.Extension;
if (fext == ".xml")
{
Console.WriteLine("Processing file:" + fi.Name);
reader = XmlReader.Create(fi.Name);
**//BUT THIS WORKS---> reader = new XmlReader(@"\\10.00.100.11 \Data\Cognos\ReportOutput\Test\Risk Rating Exception Detail (LN-133-D)-en-us_2012-04-14T031017814Z-pdf_desc.xml");**
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.Element: // The node is an element.
Console.Write("<" + reader.Name);
Console.WriteLine(">");
break;
case XmlNodeType.Text: //Display the text in each element.
Console.WriteLine(reader.Value);
break;
case XmlNodeType.EndElement: //Display the end of the element.
Console.Write("</" + reader.Name);
Console.WriteLine(">");
break;
}
}
reader.Close();
}
}
catch (System.IO.FileNotFoundException e)
{
// If file was deleted by a separate application or thread since the call to TraverseTree() then just continue.
Console.WriteLine(e.Message);
continue;
}
}
当我在单个文件上使用 XML.Create 时(请参阅 BUT THIS WORKS),我可以读取该文档中的所有内容。当我将它与 fi.Name 一起使用时,我看到消息“处理文件:”,然后,对于目录中的每个 xml 文件,我看到“找不到文件 'C:\Documents and Settings\\My Documents \Visual Studio 2010\Projects\MoveReportsTest\MoveReportsTest\bin\Debug\。
我尝试移动阅读器实例化,起初,它是为每个文件实例化的,我尝试移动 reader.Close(),认为我不能为每个文件实例化相同的阅读器,但它没有改变任何东西(同样的错误)。'找不到文件消息不是来自任何流行语......我一无所知......请帮助!谢谢!