一次从一个目录中读取多个.xml。 [英] Reading multiple .xml from a directory one at a time.

查看:38
本文介绍了一次从一个目录中读取多个.xml。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下示例代码。它从目录(固定位置)输入.xml文件,并以新格式转换现有.xml文件并将其放在d:\目录中。



现在我在源目录中有多个.xml文件。我需要一次读取每个.xml文件,将其转换为我想要的输出并将其存储到目标目录。



有些人可以帮助我做同样的事情吗?

请注意我是C#的新手。



我的代码如下。



我的尝试:



I have the following sample code.It takes .xml files input from a directory(fixed location) and convert the existing .xml file in new form and place it in d:\ directory.

Now I have multiple .xml files in the source directory. I need to read each .xml file one at a time convert the same in my desired output and store it to a destination directory.

Can some bu help me out to do the same?
Please note that I am new to C#.

My code is as follows.

What I have tried:

using.System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Schema;
using System.IO;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
       XmlDocument doc = new XmlDocument();
            doc.Load("D:\\xml_file\\abcd_present.xml");
//I think I need to write here somthing to read the files one by one through some for loop.But it is not exactly getting done.Here I require some help.
XmlTextWriter writer = new XmlTextWriter("d:\\product.xml",system.Text.Encoding.UTF8);         
            writer.WriteStartDocument(true);
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;
            writer.Flush();
            writer.WriteStartElement("root");
            // loop through each file 
           //cycle through each child node 
            String regionName = "", stationName = "", devicetype = "", device = "", startDate = "", endDate = "";
            int childNodes = doc.DocumentElement.ChildNodes.Count;
            foreach (XmlNode node in doc.DocumentElement.ChildNodes)
            {
// first node is the url ... have to go to next loc node                                 
{ 
//do existing .xml file conversion (I have the code for the same)
}
}
}
}
}

推荐答案

以下是如何处理目录中的多个文件:

Here is how to work with multiple files in a directory:
namespace ProcessDirectory
{
    internal static class Program
    {
        private static void Main(string[] args)
        {
            //var path = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            var path = @"d:\xmlfiles";

            foreach (var file in System.IO.Directory.GetFiles(path))
            {
                ProcessFile(file);
            }
            Console.ReadKey();
        }

        static void ProcessFile(string Filename)
        {
            // do processing here...
            Console.WriteLine(Filename);
        }
    }
}


感谢大家的支持。我是初学者并且学习了不同的方法来做同样的事情。我做了以下编码。

Thanks all for your support. I am a beginner and learnt different way to do the same.I have done the following coding.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.IO;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int j = 0;
            XmlDocument doc = new XmlDocument();
            var path = @"D:\XMLFiles";
            foreach (var file in System.IO.Directory.GetFiles(path))
            //doc.Load("C:\\Users\\hab77\\Desktop\\ERP xml file\\XMLFiles\\tarun20170526191139_present.xml");
            //High Alarm NTAMC.SCADA16.REGION.WR2.BOISAR400.BUS.220KV_BUS_1.HZ=50.0142 Status=GOOD
            // string path = "C:\\Users\\hab77\\Desktop\\ERP xml file\\XMLFiles";
            //foreach (string fileName in Directory.GetFiles("C:\\Users\\hab77\\Desktop\\ERP xml file\\XMLFiles", "*.xml"))
            {
                doc.Load(file);

                XmlTextWriter writer = new XmlTextWriter("d:\\ERP_XML_" + DateTime.Now.ToString("yyyyMMdd_hhmmss") + "_" + j + ".xml", System.Text.Encoding.UTF8);
                writer.WriteStartDocument(true);
                writer.Formatting = Formatting.Indented;
                writer.Indentation = 2;
                writer.Flush();
                writer.WriteStartElement("root");
                // loop through each file 
                //cycle through each child node 
                String regionName = "", substationName = "", devicetype = "", device = "", startDate = "", endDate = "";
                int childNodes = doc.DocumentElement.ChildNodes.Count;
                foreach (XmlNode node in doc.DocumentElement.ChildNodes)
                {
                    // first node is the url ... have to go to nexted loc node 
                    // foreach (XmlNode locNode1 in node)
                    // {
                    if (node.Name == "Event")
                    {
                        childNodes = node.ChildNodes.Count;
                        writer.WriteStartElement("Event");
                        foreach (XmlNode locNode in node)
                        {
                            if (locNode.Name == "TriggerData")
                            {
                                // get the content of the loc node 
                                string loc = locNode.InnerText;
                                Console.WriteLine(loc + Environment.NewLine);
                                int num = loc.IndexOf(".HZ", 0);
                                String s1 = loc.Substring(0, num);
                                num = num + 1;
                                String s2 = loc.Substring(num, loc.Length - num);//HZ and status
                                string[] status = s2.Split(' ');
                                string[] words = s1.Split('.');
                                //string[] info13 = loc.Split('.').Select(str => str.Trim()).ToArray();
                                int i = 0;
                                //String regionName = "", substationName = "", devicetype = "", device = "";
                                int len = words.Length;
                                foreach (String s in words)
                                {

                                    //XmlDocument xm = new XmlDocument();
                                    if (i % 7 == 3)
                                    {
                                        regionName = s;
                                    }

                                    if (i % 7 == 4)
                                    {
                                        substationName = s;
                                    }

                                    if (i % 7 == 5)
                                    {
                                        devicetype = s;
                                    }
                                    if (i % 7 == 6)
                                    {
                                        device = s;
                                    }
                                    //createNode(regionName, substationName, deviceName,writer);
                                    //Console.WriteLine(xm.InnerXml);
                                    //Console.ReadKey();
                                    Console.WriteLine(s + Environment.NewLine);
                                    i++;
                                    Console.WriteLine(i);
                                }
                                foreach (String s in status)
                                {
                                    int num1 = s.IndexOf("=", 0);
                                    num1++;
                                    String s3 = s.Substring(num1, s.Length - num1);
                                    if (s.Contains("HZ"))
                                    {
                                        writer.WriteStartElement("HZ");
                                        writer.WriteString(s3);
                                        writer.WriteEndElement();
                                    }
                                    if (s.Contains("Status"))
                                    {
                                        writer.WriteStartElement("Status");
                                        writer.WriteString(s3);
                                        writer.WriteEndElement();
                                    }
                                }
                                //if condition
                                writer.WriteStartElement("Region");
                                writer.WriteString(regionName);
                                writer.WriteEndElement();
                                writer.WriteStartElement("Substation");
                                writer.WriteString(substationName);
                                writer.WriteEndElement();
                                writer.WriteStartElement("devicetype");
                                writer.WriteString(devicetype);
                                writer.WriteEndElement();
                                writer.WriteStartElement("device");
                                writer.WriteString(device);
                                writer.WriteEndElement();
                                writer.WriteEndElement();
                                // write it to the console so you can see its working 
                                //Console.WriteLine(loc + Environment.NewLine);
                            }
                            if (locNode.Name == "StartDate")
                            {
                                startDate = locNode.InnerText;
                                writer.WriteStartElement("StartDate");
                                writer.WriteString(startDate);
                                writer.WriteEndElement();
                            }
                            if (locNode.Name == "EndDate")
                            {
                                endDate = locNode.InnerText;
                                writer.WriteStartElement("EndDate");
                                writer.WriteString(endDate);
                                writer.WriteEndElement();
                            }
                            // get the content of the loc node                                                     
                        }
                    }
                    // there are a couple child nodes here so only take data from node named loc
                }
                writer.WriteEndDocument();
                writer.Close();
                //}
                j++;
            }
        }
        //function to add sftp copy.
    }

}


嗨试试这个

hi try this
 XmlDocument xdoc = new XmlDocument();
                xdoc.Load(Server.MapPath("xml/sample.xml"));
                XmlNodeList NodeList = xdoc.SelectNodes("root/*");
                foreach (XmlNode Node in NodeList)
                {
                    if (Node.Attributes[0].InnerText.ToLower().Equals("p4"))
                    {
}


这篇关于一次从一个目录中读取多个.xml。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆