본문 바로가기
Language/C#

XML 파싱

by ninanio3 2017. 8. 6.

단순한 1차구조의 파싱은 여러개가 있다.

하지만 2차, 3차, 그리고 그 이상의 parsing을 해야할 때가 있다...

아래에 그 소스가 있다.

그리고 3 depth 구조 예제도 같이 올려놓겠다.

쓸데가 많을듯......

 

 

 

using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;

namespace Server.Function
{
    public class Parser
    {
        int i, j;
        private string Parse(string str)
        {
            XDocument xDoc = XDocument.Parse(str);
            Hashtable hash = new Hashtable();

            i = 0;
            parseFunction(ref hash, xDoc.Root.Elements());

            return str;
        }

        private void parseFunction(ref Hashtable hash, IEnumerable<XElement> xmlDoc)
        {
            Hashtable subHash = new Hashtable();
            List<XElement> xDocList = new List<XElement>();

            foreach (var element in xmlDoc)
            {
                if (element.HasElements == false)
                {
                    hash.Add(element.Name.LocalName, element.Value);
                    i++;
                }
                else
                {
                    foreach (var elnt in element.Elements())
                    {
                        if (elnt.HasElements == false)
                        {
                            hash.Add(elnt.Name.LocalName, elnt.Value);
                            i++;
                        }
                        else {
                            xDocList.Add(elnt);
                        }
                    }
                }
            }
            if (xDocList.Count > 0)
            {
                foreach (var xListElements in xDocList)
                {
                    subHash = (Hashtable)hash.Clone();
                    parseFunction(ref subHash, xListElements.Elements());

                    if (i < j) j = i;

                    if (subHash.Count == j)
                    {
                        overHere(subHash);
                    }
                }
            }
        }

        private void overHere(Hashtable hash) { }
    }
}

 

 

 

 

 

 


        public string Test()
        {
            XDocument srcTree = new XDocument(
                new XComment("ReceiveInsertDbData"),
                new XElement("ROOT",
                    new XElement("FACTORY_UNIT_CODE",           "THE사무실"),
                    new XElement("AREA_ID",                     "area_id"),
                    new XElement("STEP_ID",                     "step_id"),
                    new XElement("EQUIPMENT",
                        new XElement("EQP1",
                            new XElement("EQP_ID",              "eqp_id1"),
                            new XElement("EQP_TKOUT_TIME",      "eqp_tkout_time1"),
                            new XElement("EQP_TKOUT_LOT",       "eqp_tkout_lot1"),
                            new XElement("EQP_STATUS",          "eqp_status1"),
                            new XElement("test1",
                                new XElement("EQP1_test1",
                                    new XElement("test1-1",     "t1"),
                                    new XElement("test1-2",     "t2"),
                                    new XElement("test1-3",     "t3"),
                                    new XElement("test1-4",     "t4")))),
                        new XElement("EQP2",
                            new XElement("EQP_ID", "eqp_id2"),
                            new XElement("EQP_TKOUT_TIME",      "eqp_tkout_time2"),
                            new XElement("EQP_TKOUT_LOT",       "eqp_tkout_lot2"),
                            new XElement("EQP_STATUS",          "eqp_status2"),
                            new XElement("test2",
                                new XElement("EQP1_test2",
                                    new XElement("test2-1",     "t1"),
                                    new XElement("test2-2",     "t2"),
                                    new XElement("test2-3",     "t3"),
                                    new XElement("test2-4",     "t4")))),
                    new XElement("USER_ID",                     "user_id"),
                    new XElement("USER_AUTHORITY_LEVEL",        "user_authority_level"),
                    new XElement("CREATE_DATE",                 DateTime.Now.ToString("yyyyMMddHHmmss"))
                    )
                )
            );

            return srcTree.ToString();
        }