Parsing XML, Selecting node.



So there's this project that's been passed onto me on a whim. A complete newbie with XML and currently studying C#, I don't know what to make of of this project. BTW, I'm using Microsoft Visual Studio 2005. I have no other choice but to put up with this old version. So far, I've done the openfiledialog, I've done parsing. I've managed to parse my xml file and make a treeview list of it. What I was asked to do after that, was to link the objects listed in the list to the script my senior has provided.



private void Form1_Load_1(object sender, EventArgs e)
{
// Initialize the controls and the form.
textBox2.Text = Application.StartupPath + "\\TestXML.xml";
}

private void button6_Click(object sender, EventArgs e)
{
try
{
// SECTION 1. Create a DOM Document and load the XML data into it.
XmlDocument dom = new XmlDocument();
dom.Load(textBox2.Text);

// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();
/*treeView1.Nodes.Add(new TreeNode(dom.DocumentElement.Name));
TreeNode tNode = new TreeNode();
tNode = treeView1.Nodes[0];*/

foreach (XmlNode node in dom.DocumentElement.ChildNodes)
{
if (node.Name == "namespace" && node.ChildNodes.Count == 0 && string.IsNullOrEmpty(GetAttributeText(node, "name")))
continue;
AddNode(treeView1.Nodes, node);
}

treeView1.ExpandAll();
}
/* catch (XmlException xmlEx)
{
MessageBox.Show(xmlEx.Message);
}*/
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

private void LoadTreeFromXmlDocument(XmlDocument dom)
{
try
{
// SECTION 2. Initialize the TreeView control.
treeView1.Nodes.Clear();

// SECTION 3. Populate the TreeView with the DOM nodes.
foreach (XmlNode node in dom.DocumentElement.ChildNodes)
{
if (node.Name == "namespace" && node.ChildNodes.Count == 0 && string.IsNullOrEmpty(GetAttributeText(node, "name")))
continue;
AddNode(treeView1.Nodes, node);
}

treeView1.ExpandAll();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

static string GetAttributeText(XmlNode inXmlNode, string name)
{
XmlAttribute attr = (inXmlNode.Attributes == null ? null : inXmlNode.Attributes[name]);
return attr == null ? null : attr.Value;
}

private void AddNode(TreeNodeCollection nodes, XmlNode inXmlNode)
{
if (inXmlNode.HasChildNodes)
{
string text = GetAttributeText(inXmlNode, "name");
if (string.IsNullOrEmpty(text))
text = inXmlNode.Name;
TreeNode newNode = nodes.Add(text);
XmlNodeList nodeList = inXmlNode.ChildNodes;
for (int i = 0; i <= nodeList.Count - 1; i++)
{
XmlNode xNode = inXmlNode.ChildNodes[i];
AddNode(newNode.Nodes, xNode);
}
}
else
{
// If the node has an attribute "name", use that. Otherwise display the entire text of the node.
string text = GetAttributeText(inXmlNode, "name");
if (string.IsNullOrEmpty(text))
text = (inXmlNode.OuterXml).Trim();
TreeNode newNode = nodes.Add(text);
}
}


(thank you dbc for the help) This is what I've gotten so far with the XML Parsing. This is what it looks like:



_01_Test_Preparation
_01_02_Shipping_Status_Check
_01_02_Shipping_Status_Check_start
_01_01_Get_dem_ID
...
_02_Communication
_02_04_VCAN_StartLoad
_02_08_XCP_Restbus_RAM_Monitor
_02_01_VCAN_Output_Cyclic
...
...


Listed above are the test modules from the xml file. What I want to do now is that when I click/select one test module, i.e. _02_04_VCAN_StartLoad, this test module would be then active when I run a program compatible with the module. From what I've been told, listed above are all test modules for measurements and my senior wanted to do measuring specifically, not in an overall sense.


Anyway, I've googled a bit about TreeView listing and so far I've found events such as NodeMouseClick and AfterSelect. Now, I'm wondering if these two events would be useful to what I'm trying to achieve?


I've been given a starting script that I may or may not follow, it has been suggested to develop a shorter and better script than this:



private void button3_Click(object sender, EventArgs e)
{
// Start to measure
if (mMsr != null) mMsr.Start();

CANoe.System sys = null;
CANoe.Namespaces nss = null;
CANoe.Namespace ns = null;
CANoe.Variables vars = null;

try
{
// Access to System variable
sys = (CANoe.System)mApp.System;
nss = (CANoe.Namespaces)sys.Namespaces;

/**********************************************************/
/*************** [START]_01_Test_Preparation **************/
/**********************************************************/
ns = (CANoe.Namespace)nss["_01_Test_Preparation"];
vars = (CANoe.Variables)ns.Variables;

/****************************** [START]_01_01_Get_Dem_ID ******************************/
mSysVar_start = (CANoe.Variable)vars["_01_01_Get_Dem_ID_start"];
mSysVar = (CANoe.Variable)vars["_01_01_Get_Dem_ID"];
mSysVar_start.Value = 1;
int chk = 0;
System.Threading.Thread.Sleep(1000);
if ((int)mSysVar.Value != 0) while ((int)mSysVar.Value == 1 || (int)mSysVar.Value == 2) continue;
else chk = 1;
/****************************** [END] _01_01_Get_Dem_ID ******************************/

/****************************** [START]_01_02_Shipping_Status_Check *******************/
if ((int)mSysVar.Value == 3 || (int)mSysVar.Value == 4 || chk == 1)
{
if (chk == 1) chk = 0;
/*If you want to stop or start test cases at this point remove the comment out of here.
if (mMsr != null) mMsr.Stop();
System.Threading.Thread.Sleep(1000);
if (mMsr != null) mMsr.Start();
*/
mSysVar_start = (CANoe.Variable)vars["_01_02_Shipping_Status_Check_start"];
mSysVar = (CANoe.Variable)vars["_01_02_Shipping_Status_Check"];
mSysVar_start.Value = 1;
}
System.Threading.Thread.Sleep(1000);
if ((int)mSysVar.Value != 0) while ((int)mSysVar.Value == 1 || (int)mSysVar.Value == 2) continue;
else chk = 1;
/****************************** [END ]_01_02_Shipping_Status_Check *******************/
...


And the cycle repeats... This code was apparently a sample on how to call test modules from the xml but it has never been tested, yet. I was thinking of using for each, or do while for looping? I'm not entirely sure, maybe there's a better way of constructing this.


The constructed GUI so far has run and stop buttons and the treeview listing of the XML file. So it's to be expected that once a test module has been selected, clicking the run button would, of course, run it and same goes for the stop button.


Problem, I don't know where to start and what to start with. Thank you in advance.


No comments:

Post a Comment