2014-07-04 1 views
0

나와 프로그래밍 기술이 절실합니다. 내 C 드라이브에서 여러 xml 워드 프로세서를로드 할 수 있지만 루프에 무언가를 넣을 수있는 방법을 찾아서 첫 번째 코드가 아닌 모든 XML 코드를 구문 분석 할 수 있어야합니다.xmldocument를 사용하여 여러 xml 파일을 구문 분석 C#

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

가 따라서는 수집 filePaths의 첫 번째 항목 (첫 번째 파일)

나는 그것이 수집 아니기 때문에 당신을 "파일을" "파일"의 이름을 변경하는 것이 좋습니다를 기준으로있어, 그것의 한 반복 항목입니다 너의 소장품.

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

개수를 유지하려면 foreach 대신 for : int (int i = 0; i csblo

+0

완벽! 도움을 감사하십시오 !! – user3806259