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);
}
|