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();
}
}
那肯定解決了它。將整個事物放在foreach循環中是有道理的。另一個問題...我如何保持顯示每個文件的計數? – user3806259
如果你想保持計數,你必須使用:for(int i = 0; i
csblo
完美!感謝幫助! – user3806259