|
using System;
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.XPath;
using System.Globalization;
using System.Text.RegularExpressions;
namespace TODOListConverter
{
public class XmlTransformer{
private string xmlRoot, xmlTask;
private XmlDocument doc,newdoc;
private XmlDocumentFragment docfrag;
public XmlTransformer(){
xmlRoot = @"<!DOCTYPE TODOLIST [<!ELEMENT TODOLIST ANY><!ELEMENT TASK ANY><!ATTLIST TASK OUTLINENUMBER ID #REQUIRED >]><TODOLIST PROJECTNAME=""{0}"" />";
xmlTask = @"<TASK
TITLE=""{0}""
OUTLINENUMBER=""{1}""
OUTLINELEVEL=""{2}""
ID=""{3}""
PERSON=""{4}""
PERCENTDONE=""{5}""
COMMENTS=""{6}""
STARTDATESTRING=""{7}""
STARTDATE=""{8}""
DUEDATESTRING=""{9}""
EARLIESTDUEDATE=""{10}""
DUEDATE=""{10}""
PRIORITY=""{11}""
FILEREFPATH = ""{12}"" />";
doc = new XmlDocument();
newdoc = new XmlDocument();
}
private string saferow(DataRow row,string index){
try{
return row[index].ToString();
}catch{
return string.Empty;
}
}
private string FormatDate(string datetxt,out string days){
DateTime original = new DateTime(1899,12,31,0,0,0,0);
DateTime current = Convert.ToDateTime(datetxt,new DateTimeFormatInfo());
TimeSpan result = current.Subtract(original);
days = (result.Days + 1).ToString("#.00000000");
return current.ToString("MM/dd/yyyy");
}
private string GetPriority(string priority){
char[] prior = priority.ToCharArray();
if(prior.Length > 0){
return prior[0].ToString();
}else{
return "5";
}
}
private void ConcatData(DataRow row){
try{
string days = "0.00000000";
int level = Convert.ToInt32(saferow(row,"OutlineLevel"));
string comments = GetPredecessors(saferow(row,"UID")) + saferow(row,"Notes");
comments = Regex.Replace(comments,"(s*)\\r\\n(s*)","{0}");
docfrag = newdoc.CreateDocumentFragment();
docfrag.InnerXml = string.Format(xmlTask,
saferow(row,"Name").Replace("&","&"),
saferow(row,"OutlineNumber"),
saferow(row,"OutlineLevel"),
(Convert.ToInt32(saferow(row,"ID"))+1),
GetResourceName(saferow(row,"UID")),
saferow(row,"PercentWorkComplete"),
comments,
FormatDate(saferow(row,"Start"),out days),
days,
FormatDate(saferow(row,"Finish"),out days),
days,
GetPriority(saferow(row,"Priority")),
saferow(row,"HyperlinkAddress")
);
if(level == 0){
newdoc.DocumentElement.AppendChild(docfrag);
}else if(level == 1){
newdoc.DocumentElement.FirstChild.AppendChild(docfrag);
}else{
string[] outlineno = saferow(row,"OutlineNumber").Split('.');
string temp = string.Join(".",outlineno,0,outlineno.Length-1);
newdoc.GetElementById(temp).AppendChild(docfrag);
}
}catch(Exception e){
Console.WriteLine(e.Message);
}catch{
Console.WriteLine("Error Occurred");
}
}
private string GetPredecessors(string TaskUID){
string PredecessorsID = string.Empty;
XmlNodeList tasknode;
XmlNode resnode = null;
try{
tasknode = doc.SelectNodes("/Project/Tasks/Task/UID");
foreach(XmlNode node in tasknode){
if(node.InnerText == TaskUID){
resnode = node.SelectSingleNode("parent::Task/PredecessorLink/PredecessorUID");
break;
}
}
if(resnode != null){
foreach(XmlNode node in tasknode){
if(node.InnerText == resnode.InnerText){
PredecessorsID = node.ParentNode.ChildNodes[1].InnerText;
break;
}
}
if(PredecessorsID != null && PredecessorsID.Length > 0){
int tempid = (Convert.ToInt32(PredecessorsID) + 1);
PredecessorsID = string.Format("Predecessor Node : tdl://{0} {1}",tempid,"{0}") ;
}else{
PredecessorsID = string.Empty;
}
}
}catch(Exception e){
Console.WriteLine(e.Message);
}catch{
Console.WriteLine("Error Occurred");
}
return PredecessorsID;
}
private string GetResourceName(string TaskUID){
string ResName = string.Empty;
XmlNodeList tasknode,residnode;
XmlNode resnode = null;
XmlNode resourcenode = null;
try{
tasknode = doc.SelectNodes("/Project/Assignments/Assignment/TaskUID");
foreach(XmlNode node in tasknode){
if(node.InnerText == TaskUID){
resnode = node.SelectSingleNode("parent::Assignment/ResourceUID");
break;
}
}
if(resnode != null){
residnode = doc.SelectNodes("/Project/Resources/Resource/UID");
foreach(XmlNode node in residnode){
if(node.InnerText == resnode.InnerText){
resourcenode = node.SelectSingleNode("parent::Resource");
break;
}
}
if(resourcenode != null){
ResName = resourcenode.SelectSingleNode("Name").InnerText;
}else{
ResName = string.Empty;
}
}
}catch(Exception e){
Console.WriteLine(e.Message);
}catch{
Console.WriteLine("Error Occurred");
}
return ResName;
}
public string Transform(string xmlstring){
string xmlstr = string.Empty;
try{
doc.LoadXml(xmlstring);
try
{
xmlstring = doc.SelectSingleNode("/Project/Title").InnerText;
}
catch
{
xmlstring = doc.SelectSingleNode("/Project/Name").InnerText;
}
newdoc.LoadXml(string.Format(xmlRoot,xmlstring));
DataSet ds = new DataSet();
ds.ReadXml(new StringReader(doc.SelectNodes("/Project/Tasks").Item(0).OuterXml));
foreach(DataRow row in ds.Tables[0].Rows){
ConcatData(row);
}
xmlstr = newdoc.InnerXml;
#if DEBUG
Console.WriteLine(newdoc.InnerXml);
#endif
}catch(Exception e){
Console.WriteLine("Error : File not in a proper format");
}catch{
Console.WriteLine("Error Occurred");
}
return xmlstr;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I have been working in software industry for the past 11 years, though i am trained to be a pharmacist. Enjoy working on complex and new technologies. Also cleared my SCJP and MCP certification examinations.