SPATIUM Mobile
주소복사
About Operating System Languages Tools Favorites Notice Visit TEST  
     ADK (3)
     android (3)
     ASP (5)
     ASP.Net (1)
     C# (5)
     HTML (9)
     JAVA (7)
     JavaScript (9)
     jQuery (5)
     Mobile Web (2)
     node.js (1)
     Oracle (8)
     PHP (7)
     Spring Framework (4)
     SQL (17)
     WPF (5)
   ID  
   Password  
  |  
  Location United States
  IP Address 3.145.176.228
2024. 11
12
3456789
10111213141516
17181920212223
24
252627282930
Category  Languages, ASP.Net
Writer 김태우 Date 2013-11-04 11:33:39 Visit 3465
ASP.Net Xml Parse

 

XML Parse in ASP.Net 

XML source

<?xml version="1.0" encoding="utf-8" ?>
<Site Name="HomePage">
  <Folder Name="회사 소개" Url="/Company">
    <SubFolder Name="회사 이력" Url="/Company/History"></SubFolder>
    <SubFolder Name="CEO 인사말" Url="/Company/CEO"></SubFolder>
  </Folder>
  <Folder Name="사업 영역" Url="/Business">
    <SubFolder Name="온라인" Url="/Business/Online"></SubFolder>
    <SubFolder Name="오프라인" Url="/Business/Offline"></SubFolder>
  </Folder>
</Site>

하위 코드를 사용

public static class SiteMapXmlLoader
    {
        public static void ReadXml(SiteMap siteMap)
        {
            XmlReader reader = XmlReader.Create(HttpContext.Current.Server.MapPath("~/Contents/SiteMap.xml"));
            
            if (siteMap.FolderList == null)
            {
                siteMap.FolderList = new List<Folder>();
            }
            else
            {
                siteMap.FolderList.Clear();
            }

            lock (reader)
            {
                
                while (reader.Read())
                {
                    if (reader.IsStartElement("Folder"))
                    {
                        Folder folder = new Folder();
                        folder.FolderName = String.IsNullOrEmpty(reader.GetAttribute("Name")) ? "" : reader.GetAttribute("Name");
                        folder.Url = String.IsNullOrEmpty(reader.GetAttribute("Url")) ? "" : reader.GetAttribute("Url");
                        folder.ParentFolder = null;
                        folder.SubFolderList = new List<Folder>();

                        if (reader.ReadToDescendant("SubFolder"))
                        {
                             do
                             {
                                 Folder subFolder = new Folder();
                                 subFolder.FolderName = String.IsNullOrEmpty(reader.GetAttribute("Name")) ? "" : reader.GetAttribute("Name");
                                 subFolder.Url = String.IsNullOrEmpty(reader.GetAttribute("Url")) ? "" : reader.GetAttribute("Url");
                                 subFolder.ParentFolder = folder;

                                 folder.SubFolderList.Add(subFolder);
                             }
                             while (reader.ReadToNextSibling("SubFolder"));
                        }
                        siteMap.FolderList.Add(folder);
                    }
                    if (reader.Name == "Site")
                    {
                        siteMap.SiteName = String.IsNullOrEmpty(reader.GetAttribute("Name")) ? "" : reader.GetAttribute("Name");
                    }
                }
            }
        }

        public static Classes.SiteMap LoadMenu()
        {
            Classes.SiteMap siteMap;// = new Classes.SiteMap();

            Cache cache = HttpContext.Current.Cache;

            if (cache["SiteMap"] == null)
            {
                siteMap = new Classes.SiteMap();
                SiteMapXmlLoader.ReadXml(siteMap);
                cache.Insert("SiteMap", siteMap, null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0));
            }
            else
            {
                siteMap = (Classes.SiteMap)cache["SiteMap"];
            }
            return siteMap;
        }

        public static Classes.Folder GetPageNamePath()
        {
            // Load Xml at Cache
            Classes.SiteMap siteMap = Classes.SiteMapXmlLoader.LoadMenu();

            Classes.Folder retValue = null;

            //String url = HttpContext.Current.Request.Url.AbsoluteUri;       // http://localhost:9721/Sample/SiteMap.aspx
            String path = HttpContext.Current.Request.Url.AbsolutePath;     // /Sample/SiteMap.aspx

            String[] stringSeparators = new string[] { "/" };
            String[] urlFolders = path.Split(stringSeparators, StringSplitOptions.None);

            if (urlFolders.Length > 2)
            {
                foreach (Classes.Folder folder in siteMap.FolderList)
                {
                    if (folder.Url == "/" + urlFolders[1])
                    {
                        if (urlFolders.Length > 3)
                        {
                            foreach (Classes.Folder subfolder in folder.SubFolderList)
                            {
                                if (subfolder.Url == "/" + urlFolders[1] + "/" + urlFolders[2])
                                {
                                    return subfolder;
                                }
                            }
                        }
                        return folder;
                    }
                }
            }

            return retValue;
        }

    }

실제 사용

Classes.Folder folder = Classes.SiteMapXmlLoader.GetPageNamePath();

            if (folder != null)
            {
                if (folder.ParentFolder != null)
                {
                    Response.Write(folder.ParentFolder.FolderName);
                    Response.Write(folder.ParentFolder.Url);
                }
                Response.Write(folder.FolderName);
                Response.Write(folder.Url);
            }
Tags  Xml Parse, SiteMap
  Relation Articles
[Languages-ASP.Net] ASP.Net Xml Parse (2013-11-04 11:33:39)
  Your Opinion
Member ID
150 letters
Copyright (C) SPATIUM. All rights reserved.
[SPATIUM]WebMaster Mail