[英]Parsing multiple xml files using xmldocument c#
I am able to load multiple xml docs from my c drive but I need to find a way to put something into a loop so I can parse all of the xml's rather than just the first one. 我可以从C驱动器中加载多个xml文档,但是我需要找到一种将某些内容放入循环中的方法,这样我就可以解析所有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();
}
}
You have forget the scope of : 您忘记了:
foreach (string files in filePaths)
And you must load your document with : 并且您必须使用以下方式加载文档:
doc.Load(files);
Instead of 代替
doc.Load(file);
Because file is referenced to 因为文件被引用
string file = filePaths[0].ToString();
Therefore it's referenced to the first item of the collection filePaths (the first file) 因此,它引用了集合文件路径的第一项(第一个文件)
I suggest you to rename "files" to "file" because it's not a collection, it's just one iterated items of your collection. 我建议您将“文件”重命名为“文件”,因为它不是集合,而只是集合中的一个迭代项。
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.