그냥저냥

XML 파싱 및 파일명 변경 본문

Dev./C#

XML 파싱 및 파일명 변경

ex3llo 2016. 12. 28. 15:37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;
using System.Collections;
 
namespace Name_Parsing {
    public partial class Form1 : Form {
        public ListViewItem li = null;
        public ListView lv = null;
        string[] test;
        int index = 0;
        string cMD5 ="";
        string cITEM = "";
        string cNAME = "";
        string oldName = "";
        string newName = "";
        string result = "";
        public Form1() {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e) {
            li = new ListViewItem();
            DirectoryInfo dirCheck = new DirectoryInfo(@"파일 경로(폴더까지만)");
            if(dirCheck.Exists == false){
                MessageBox.Show("[Usage]₩r₩n1.C드라이브에 XML폴더 생성₩r₩n2.해당 경로에 파일 복사₩r₩n3.프로그램 재실행");
                Application.ExitThread(); //프로그램 강제 종료
            }
            String[] searchFile = Directory.GetFiles(@"파일경로",".확장자");
        
            // 파일내용조정(파싱 전처리작업)
            foreach(FileInfo f in di.GetFiles()){
                if(f.Extension == ".xml") {
                    try {
                        StreamReader reader = new StreamReader(@"파일경로"+f.Name);
                        string content = reader.ReadToEnd();
                        reader.Close();
                        content = content.Replace("<?xml version=₩"1.0₩" ?>""");
                        content = content.Replace("<?xml version=₩"1.0₩" encoding=₩"utf-8₩"?>""");
                        content = "<parsing>"+content+"</parsing>";
                        StreamWriter writer = new StreamWriter(@"파일경로"+f.name);
                        writer.Write(content);
                        writer.Close();
                        index++;
                    } catch (Exception ex) {
                        MessageBox.Show(ex.ToString());
                    }
                }
            }
 
            // 파싱 작업
            String[][] list = new String[index][];
            DirectoryInfo dir = new DirectoryInfo(@"파일경로");
            int i = 0;
            foreach(FileInfo fi in dir.GetFiles()){
                if(fi.Extension == ".xml"){
                    oldName = fi.Name;
                    try{
                        cMD5 = "";
                        cITEM = "";
                        cNAME = "";
                        parsing(@"파일경로"+fi.Name);
                        for(int j=0; j<searchFile.length; j++){
                            if(cITEM != ""){
                                if(searchFile[j].IndexOf(cITEM) != -1){
                                    FileInfo fileInfo = new FileInfo(searchFile[j]);
                                    fileInfo.MoveTo("파일경로"+cNAME);
                                }
                            } else {
                                if(searchFile[j].IndexOf(cMD5) != -1){
                                    FileInfo fileInfo = new FileInfo(searchFile[j]);
                                    fileInfo.MoveTo("파일경로"+cNAME);
                                }
                            }
                            newName = cNAME;
                        }
                        Application.ExitThread();
                    } catch (Exception ex) {
                        Console.WriteLine(ex.ToString());
                    }
                    result += oldName+" >>> " +newName+"₩r₩n";
                }
                i++;
            }
 
            string savePath = @"로그파일저장경로";
            System.IO.File.WriteAllText(savePath,result, Encoding.Default);
            System.Diagnostics.Process.Start("notepad.exe",@"로그파일 저장경로");
            String[] deleteFile = Directory.GetFiles(@"파일경로","*.xml");
            for(int k=0,k<deleteFile.Length;k++)
            File.Delete(deleteFile[k]);
            Application.ExitThread();
        }
 
        public void parsing(string xmlFile) {
            try {
                XmlDocument xml = new XmlDocument();
                xml.Load(xmlFile);
                XmlNodeList eMD5 = xml.GetElementsByTagName("파싱할 태그1");
                XmlNodeList eITEM = xml.GetElementsByTagName("파싱할 태그2");
                XmlNodeList eNAME = xml.GetElementsByTagName("파싱할 태그3");
 
                try {
                    cMD5 = eMD5.Item(0).InnerText;
                    cITEM = eITEM.Item(0).InnerText;
                    cNAME = eNAME.Item(0).InnerText;
                } catch {}
            } catch (Exception ex) {
                Console.WriteLine("XML 파싱 에러 : "+ex);
            }
        }
    }
}
cs


'Dev. > C#' 카테고리의 다른 글

Ping 프로그램 코드  (0) 2016.12.28
Comments