2014-07-04 44 views
0

裸露與我和我的編程技能。我可以從我的C盤加載多個XML文檔,但我需要找到一種方法將某些東西放入循環中,這樣我就可以解析所有的XML而不僅僅是第一個。使用xmldocument c解析多個xml文件#

using System; 
using System.IO; 
using System.Xml; 

public class Program 
{ 


    public static void Main() 
    { 

     string[] filePaths = Directory.GetFiles(@"C:\Users\r626890\Documents\BoxTesting\",  "*.xml"); 
     foreach (string files in filePaths) 
     Console.WriteLine("'{0}'",files); 
     Console.WriteLine(); 

     XmlDocument doc = new XmlDocument(); 
     string file = filePaths[0].ToString(); 
     XmlNodeList xmlnode; 
     doc.Load(file); 

     XmlNodeList indexValue = doc.GetElementsByTagName("Index"); 

     for (int i = 0; i < indexValue.Count; i++) 
     { 
      Console.WriteLine(); 
      Console.WriteLine("Description: {0} ",    indexValue[i].ChildNodes.Item(0).InnerXml); 
      Console.WriteLine("Value:  {0} ", indexValue[i].ChildNodes.Item(1).InnerXml); 
     // Console.WriteLine("{0}",i); 
     } 

     xmlnode = doc.GetElementsByTagName("FileName"); 
     for (int i = 0; i < xmlnode.Count; i++) 
     { 
      // Console.WriteLine("URI: {0} ", elemList[i].InnerXml.Split('_')[0]); 
      Console.WriteLine(); 
      Console.WriteLine("Type:  {0} ", xmlnode[i].InnerXml.Split('_')[1]); 
      Console.WriteLine(); 
      Console.WriteLine("File Name: {0} ", xmlnode[i].InnerXml); 
          Console.WriteLine("------------------------------------------------------------"); 
      Console.WriteLine("(Total # of files: {0})", ++i); 
     } 



     Console.ReadLine(); 

    } 
} 

回答

0

你必須忘記的範圍:

foreach (string files in filePaths) 

而且你必須裝入原稿:由於文件被引用到

doc.Load(files); 

而不是

doc.Load(file); 

string file = filePaths[0].ToString(); 

因此它引用到集合文件路徑的第一項(第一檔)

我建議你重命名「文件」,「文件」,因爲它不是一個集合,它只是一個迭代的項目你的收藏。

string[] filePaths = Directory.GetFiles(@"C:\Users\r626890\Documents\BoxTesting\",  "*.xml"); 
    foreach (string file in filePaths) 
    { 
     Console.WriteLine("'{0}'",file); 
     Console.WriteLine(); 

     XmlDocument doc = new XmlDocument(); 
     //string file = filePaths[0].ToString(); 
     XmlNodeList xmlnode; 
     doc.Load(file); 

     XmlNodeList indexValue = doc.GetElementsByTagName("Index"); 

     for (int i = 0; i < indexValue.Count; i++) 
     { 
      Console.WriteLine(); 
      Console.WriteLine("Description: {0} ",    indexValue[i].ChildNodes.Item(0).InnerXml); 
      Console.WriteLine("Value:  {0} ", indexValue[i].ChildNodes.Item(1).InnerXml); 
     // Console.WriteLine("{0}",i); 
     } 

     xmlnode = doc.GetElementsByTagName("FileName"); 
     for (int i = 0; i < xmlnode.Count; i++) 
     { 
      // Console.WriteLine("URI: {0} ", elemList[i].InnerXml.Split('_')[0]); 
      Console.WriteLine(); 
      Console.WriteLine("Type:  {0} ", xmlnode[i].InnerXml.Split('_')[1]); 
      Console.WriteLine(); 
      Console.WriteLine("File Name: {0} ", xmlnode[i].InnerXml); 
          Console.WriteLine("------------------------------------------------------------"); 
      Console.WriteLine("(Total # of files: {0})", ++i); 
     } 

    } 

    Console.ReadLine(); 
+0

那肯定解決了它。將整個事物放在foreach循環中是有道理的。另一個問題...我如何保持顯示每個文件的計數? – user3806259

+0

如果你想保持計數,你必須使用:for(int i = 0; i csblo

+0

完美!感謝幫助! – user3806259