본문 바로가기
Language/C#

c# parsing

by ninanio3 2012. 1. 2.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;

namespace Exercise
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"C:\parsingfile.txt";
            string temp = "";
            string temp2 = "";
            string temp3 = "";
            string temp4 = "";
            string hello = "";
            string[] vHello = new string[2];
            string[] vSize = new string[2];
            string[] vColor = new string[2];

            StreamReader sr = null;
             string line = "";
             string[] itemValue;

            sr = new StreamReader(path);
            try
           {
                while (!sr.EndOfStream) 
                {
                     line = sr.ReadLine().Replace("<", "").Replace(">", "").Replace("?", "").Replace("/></font><br/>", "");
                     int cnt = line.LastIndexOf("hello");
                     int cnt2 = line.LastIndexOf("size");
                     int cnt3 = line.LastIndexOf("color");
                     int cnt4 = line.LastIndexOf("name");
                     if (cnt > 0)
                     {
                         //Hello 앞뒤 나누기
                         temp = line.Substring(cnt);  //Hello 후 <- 값있음
                         temp2 = line.Substring(0, cnt - 1);  //Hello 전
                         hello = temp.Substring(0, temp.IndexOf("/"));
                         vHello = hello.Split('=');
                         Console.WriteLine("{0} = {1}", vHello[0], vHello[1]);

                        //size 앞뒤 나누기
                         if (cnt2 > 0)
                         {
                             temp3 = temp2.Substring(cnt2).Replace("xsl:value-of", "");  //Size 후 <- 값있음
                             temp4 = temp2.Substring(0, cnt2 - 1);  //Size 전

                            vSize = temp3.Split('=');
                            Console.WriteLine("{0} = {1}", vSize[0], vSize[1]);
                            //color 앞뒤 나누기
                            if (cnt3 > 0)
                            {
                                temp4 = temp4.Replace("\t", "");
                                vColor = temp4.Split('=');
                                Console.WriteLine("{0} = {1}", vColor[0], vColor[1]);
                            }
                        }
                    }
                    else if (cnt4 > 0)
                    {
                        temp = line.Substring(cnt4);
                        itemValue = temp.Split('=');
                        Console.WriteLine("{0} = {1}", itemValue[0], itemValue[1]);
                    }
                    Console.WriteLine();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
       }
    }
}

 

참고자료
http://ocplay.springnote.com/pages/1771134?print=1

ArrayList a = new ArrayList[]
a = line.split(' ')  //공백으로 구분

for(int i = 0; i<a.Count; i++)
aa = a[i].Tostring().split('=')
if(aa<1)

innertext