Traverse Files and Folders and XML Generation
Hi , hope you all are fine and doing well. Actually i am stuck in a problem. I want to generate a xml schema of files and folders. Situation is that i can pass any folder's path. It will work as ROOT. Then it will search all the files and folders in it and generate a schema for that. I have been able to traverse all the files and Folders and Fetching their attributes and saving to a string. But real problem is that how do i set parent ID of files and Folder. Like if there is a file
c:\My Documents\a.jpg
if i have set My Documents id = 1 then a.jpg's parent id should be 1. and its own id could be any thing, I hope you have got my point. Problem is in parentID how do i assign my childs correct parent ID. Currently it generates the Schema but parentID is not correct in many cases. Here is a function have a look.
public void outputXMLString(string folder)
{
string txt = ".txt";
string doc = ".doc";
string pdf = ".pdf";
string jpg = ".JPG";
string jpeg = ".JPEG";
string jpg1 = ".jpg";
string jpeg1 = ".jpeg";
string bmp = ".bmp";
string xls = ".xls";
string ppt = ".ppt";
string wav = ".wav";
string mp3 = ".mp3";
string mid = ".mid";
string mpeg = ".mpeg";
string mpg = ".mpg";
string wmv = ".wmv";
string avi = ".avi";
try
{
DirectoryInfo dir = new DirectoryInfo(folder);
DirectoryInfo[] subdir = dir.GetDirectories();
int parent = 0;
int id = 0;
//ArrayList bfs = new ArrayList();
//FolderInformation objFolderInfo = new FolderInformation();
//FileInformation objFileInfo = new FileInformation();
// string xmlDocString = "";
string path;
string[] fname = null;
//objFolderInfo.setName(dir.Name);
//objFolderInfo.setParentId(id.ToString());
//objFolderInfo.setPath(extractPath(dir.FullName));
//objFolderInfo.setLastModified(dir.LastAccessTime.ToShortDateString() + " " + dir.LastAccessTime.ToShortTimeString());
//objFolderInfo.setId(id.ToString());
//objFolderInfo.setDirObject(dir);
//objFolderInfo.getFoldersList().Add(objFolderInfo);
parent = 0;
foreach (DirectoryInfo sd in subdir)
{
id++;
path = extractPath(sd.FullName);
// parent = findParent(extractPath(sd.FullName));
xmlDocString += "";
xmlDocString += AddXmlElement("id", id.ToString());
//xmlDocString += AddXmlElement("id", idenity.ToString());
xmlDocString += AddXmlElement("name", sd.Name);
xmlDocString += AddXmlElement("parent", parent.ToString());
xmlDocString += AddXmlElement("lastModified", sd.LastAccessTime.ToShortDateString() + " " + sd.LastAccessTime.ToShortTimeString());
xmlDocString += AddXmlElement("path", path);
parent = id;
AddElementsString(sd.FullName);
xmlDocString += "";
FileInfo[] files = sd.GetFiles();
foreach (FileInfo fi in files)
{
if (fi.Extension.ToLower().Equals(txt) ||
fi.Extension.ToLower().Equals(doc) ||
fi.Extension.ToLower().Equals(pdf) ||
fi.Extension.ToLower().Equals(jpg) ||
fi.Extension.ToLower().Equals(jpeg) ||
fi.Extension.ToLower().Equals(jpg1) ||
fi.Extension.ToLower().Equals(jpeg1) ||
fi.Extension.ToLower().Equals(bmp) ||
fi.Extension.ToLower().Equals(xls) ||
fi.Extension.ToLower().Equals(ppt) ||
fi.Extension.ToLower().Equals(wav) ||
fi.Extension.ToLower().Equals(mp3) ||
fi.Extension.ToLower().Equals(mpeg) ||
fi.Extension.ToLower().Equals(mid) ||
fi.Extension.ToLower().Equals(wmv) ||
fi.Extension.ToLower().Equals(avi) ||
fi.Extension.ToLower().Equals(mpg)
)
{
path = extractPath(fi.FullName);
//parent = findParent(completePath(fi.FullName));
fname = fi.Name.Split('.');
xmlDocString += "";
xmlDocString += AddXmlElement("name", fname[0]);
xmlDocString += AddXmlElement("parent", parent.ToString());
xmlDocString += AddXmlElement("type", fi.Extension.Substring(1));
xmlDocString += AddXmlElement("lastModified", fi.LastAccessTime.ToShortDateString() + " " + fi.LastAccessTime.ToShortTimeString());
xmlDocString += AddXmlElement("path", path);
xmlDocString += "";
}
}
}
//foreach (DirectoryInfo dir in bfs)
//{
// subdir = dir.GetDirectories();
// foreach (DirectoryInfo d in subdir)
// {
// bfs.Add(d);
// }
//}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}
Your Help is always appreciated
Regards,
Adnan