XPDL Writer : XML Processor in C#

Today lately i have been working on my FYP!.. was going through XML Writing and found out it’s quite complex when it comes for n00b to write an XML document in C# Or perhaps it would be just because i haven’t being programming in C# for a while now.

So What i have done is to come up with a Class that will do pretty much of XML work Easily!

static class XMLProcessor
{

public static XmlDocument doc = new XmlDocument();
public static string filePATH = “”;
public static bool LoadDocument(string PATH) {

try
{
if (filePATH == “”)
filePATH = PATH;

doc.Load(PATH);

}
catch (Exception ex)
{
if (ex.Message.ToString().Contains(“Could not find”))
{

XmlDocument doc = new XmlDocument();
XmlNode docNode = doc.CreateXmlDeclaration(“1.0”, “UTF-8”, null);
doc.AppendChild(docNode);
XmlNode WFProcessNode = doc.CreateElement(“WorkflowProcess”);
doc.AppendChild(WFProcessNode);

XmlNode ActivitiesNode = doc.CreateElement(“Activities”);
XmlNode ParticipantsNode = doc.CreateElement(“Participants”);
XmlNode TransitionsNode = doc.CreateElement(“Transitions”);
XmlNode ExtendedAttributesNode = doc.CreateElement(“ExtendedAttributes”);

// Have to do it b/c It wasnt making element like <element></element> 🙂
ActivitiesNode.InnerText = “”;
ParticipantsNode.InnerText = “”;
TransitionsNode.InnerText = “”;
ExtendedAttributesNode.InnerText = “”;

// Template for the Workflow document.!
WFProcessNode.AppendChild(ParticipantsNode);
WFProcessNode.AppendChild(ActivitiesNode);
WFProcessNode.AppendChild(TransitionsNode);
WFProcessNode.AppendChild(ExtendedAttributesNode);
// Template ends here

doc.Save(PATH);
filePATH = PATH;
LoadDocument(PATH);
}

}

return true;
}

public static XmlElement CreateElement(string variable)
{
XmlElement empnode = doc.CreateElement(variable);
return empnode;
}

public static XmlNode CreateNode(string variable, string value)
{
XmlNode tempnode = doc.CreateNode(System.Xml.XmlNodeType.Element,variable,””);
tempnode.InnerText = value;
return tempnode;

}

public static XmlNode CreateNode(string variable)
{
XmlNode tempnode = doc.CreateElement(variable);
return tempnode;

}

public static void AddAttribute(XmlNode Node, string attributename, string attributeValue)
{
XmlAttribute Attribute = doc.CreateAttribute(attributename);
Attribute.Value = attributeValue;
Node.Attributes.Append(Attribute);
}

}

Now a simple call to XMLProcessor.LoadDocument(“C:\\file.xpdl”);

Would create or open an xpdl which is refined form of XML for workflow process !

// Adding a <Node></Node> Is as simple as that

XmlNode parent = XMLProcessor.doc.DocumentElement;
XmlNode Node = XMLProcessor.CreateNode(“Node”);
parent.AppendChild(Node);
XMLProcessor.doc.Save(XMLProcessor.filePATH);

//  And a new Child would be added for Root Node Similarly You can use XPath and go through the XML and add nodes Like an excellent guide to Selecting Nodes  can be found here

XmlNodeList List = XMLProcessor.doc.SelectNodes(“/WorkflowProcess/Transitions”);

Will Go into <WorkflowProcess> and would select all the <Transitions> Nodes!

and in that list you can get a parent node via

XmlNode parent = List[position of parent];

// Adding a node with attributes.!

XmlNode Node = XMLProcessor.CreateNode(“Transition”);
XMLProcessor.AddAttribute(Node, “From”, “fromactivity”);
XMLProcessor.AddAttribute(Node, “To”, “toactivity”);
XMLProcessor.AddAttribute(Node, “Id”, “3”);
XmlNode parent = XMLProcessor.doc.DocumentElement;
parent.AppendChild(Node);
XMLProcessor.doc.Save(XMLProcessor.filePATH);

And a node <Transition From=”fromactivity” To=”toactivity” Id=”3″ />

would be added!

Don’t forget to do XMLProcessor.doc.Save(XMLprocessor.filePATH);

inorder to save the out put !

All you have to do is to change the definition of LoadDocument To suite your XML Schema.! 🙂

Signing off!

Azeem.

Advertisements

~ by Azeem on December 26, 2007.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: