anatar02
13 years agoContributor
Required help to get the xml node value by using C#
Hi all, I am very new to C# scripting and I have written the function for retrieving the node values from xml with help of testcomplete helper!! but unfortunately, I did not get what i supposed to get.
I want my function to retrieve the particular node values not all the node values. the below code has given all the node values but not particular node, so please have a look into my xml and code and help me on the same.
xml......
*********************************************************
<TestData>
<Data>
<Name>data_txtBankNumber</Name>
<Value>17</Value>
</Data>
<Data>
<Name>LastName</Name>
<Value>LastName</Value>
</Data>
<Data>
<Name>Company</Name>
<Value>test</Value>
</Data>
</TestData>
********************************************************
code...
function TestWithXPath()
{
var Doc, s, Nodes, ChildNodes, i, Node,onodelist,onodelistr,Labels,Coll;
// Create COM object
// If you have MSXML 4:
Doc = Sys["OleObject"]("Msxml2.DOMDocument.4.0");
// If you have MSXML 6:
Doc = Sys["OleObject"]("Msxml2.DOMDocument.6.0");
Doc["async"] = false;
// Load data from a file
// We use the file created earlier
Doc["load"]("C:\\Data.xml");
// Report an error, if, for instance, the markup or file structure is invalid
if(Doc["parseError"]["errorCode"] != 0)
{
s = "Reason:\t" + Doc["parseError"]["reason"] + "\n" +
"Line:\t" + aqConvert["VarToStr"](Doc["parseError"]["line"]) + "\n" +
"Pos:\t" + aqConvert["VarToStr"](Doc["parseError"]["linePos"]) + "\n" +
"Source:\t" + Doc["parseError"]["srcText"];
// Post an error to the log and exit
Log["Error"]("Cannot parse the document.", s);
return;
}
// Use an XPath expression to obtain a list of "control" nodes
Nodes = Doc["selectNodes"]("//TestData");
// Process the node
for(i = 0; i < Nodes["length"]; i++)
{
// Get the node from the collection of found nodes
Node = Nodes["item"](i).text;
// Split the full label into parts
Labels = Node["split"](".");
// Get child nodes
var rest=Nodes["nextNode"]("data_txtBankNumber")
Log["Message"](rest);
// Log["Message"]aqString["GetListItem"]("data_txtBankNumber",i);
//Log["Message"]( aqString["GetListItem"](Node,i));
// Log["Message"]( aqString["GetListItem"](Labels,"data_txtBankNumber"))
// Output two child nodes to the log
//Log["Message"](ChildNodes["item"](1)["text"] + ": " + ChildNodes["item"](2)["text"]);
//for (var j = 0; j < aqString["GetLength"](Node); j++)
//Log["Message"]( aqString["GetListItem"](Node,i));
}
}
I want my function to retrieve the particular node values not all the node values. the below code has given all the node values but not particular node, so please have a look into my xml and code and help me on the same.
xml......
*********************************************************
<TestData>
<Data>
<Name>data_txtBankNumber</Name>
<Value>17</Value>
</Data>
<Data>
<Name>LastName</Name>
<Value>LastName</Value>
</Data>
<Data>
<Name>Company</Name>
<Value>test</Value>
</Data>
</TestData>
********************************************************
code...
function TestWithXPath()
{
var Doc, s, Nodes, ChildNodes, i, Node,onodelist,onodelistr,Labels,Coll;
// Create COM object
// If you have MSXML 4:
Doc = Sys["OleObject"]("Msxml2.DOMDocument.4.0");
// If you have MSXML 6:
Doc = Sys["OleObject"]("Msxml2.DOMDocument.6.0");
Doc["async"] = false;
// Load data from a file
// We use the file created earlier
Doc["load"]("C:\\Data.xml");
// Report an error, if, for instance, the markup or file structure is invalid
if(Doc["parseError"]["errorCode"] != 0)
{
s = "Reason:\t" + Doc["parseError"]["reason"] + "\n" +
"Line:\t" + aqConvert["VarToStr"](Doc["parseError"]["line"]) + "\n" +
"Pos:\t" + aqConvert["VarToStr"](Doc["parseError"]["linePos"]) + "\n" +
"Source:\t" + Doc["parseError"]["srcText"];
// Post an error to the log and exit
Log["Error"]("Cannot parse the document.", s);
return;
}
// Use an XPath expression to obtain a list of "control" nodes
Nodes = Doc["selectNodes"]("//TestData");
// Process the node
for(i = 0; i < Nodes["length"]; i++)
{
// Get the node from the collection of found nodes
Node = Nodes["item"](i).text;
// Split the full label into parts
Labels = Node["split"](".");
// Get child nodes
var rest=Nodes["nextNode"]("data_txtBankNumber")
Log["Message"](rest);
// Log["Message"]aqString["GetListItem"]("data_txtBankNumber",i);
//Log["Message"]( aqString["GetListItem"](Node,i));
// Log["Message"]( aqString["GetListItem"](Labels,"data_txtBankNumber"))
// Output two child nodes to the log
//Log["Message"](ChildNodes["item"](1)["text"] + ": " + ChildNodes["item"](2)["text"]);
//for (var j = 0; j < aqString["GetLength"](Node); j++)
//Log["Message"]( aqString["GetListItem"](Node,i));
}
}