Forum Discussion

anatar02's avatar
anatar02
Contributor
13 years ago

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));



  }

}

4 Replies

  • Thanks for your reply lorenzo :)



    I have tried with the same, but nu luck!!!..



    No Error message. nothing.
  • Julia_K's avatar
    Julia_K
    SmartBear Alumni (Retired)

    Hello Ashok,

    To access a particular node whose value you want to obtain, you can use the XPath expression just like Lorenzo suggested.

    You can find the detailed information on the XPath expression syntax in MSDN library: http://msdn.microsoft.com/en-us/library/ms256471.aspx and http://msdn.microsoft.com/en-us/library/ms256086.aspx.


    For example, to obtain the node with the data_txtBankNumber value, you can use the //TestData/Data[Name='data_txtBankNumber']/Name or //TestData/Data[1]/Name XPath expression.

    The following sample script obtains the Name and Value nodes and posts their values to the test log:



    function TestWithXPath()

    {

      var Doc, s, Nodes, Node;

      

      Doc = Sys["OleObject"]("Msxml2.DOMDocument.4.0");

      Doc["async"] = false;

      Doc["load"]("C:\\Data.xml");

     

      // Use the XPath expression to obtain the needed node

      NameNode = Doc["selectSingleNode"]("//TestData/Data[Name='data_txtBankNumber']/Name");

      ValueNode = Doc["selectSingleNode"]("//TestData/Data[Name='data_txtBankNumber']/Value");

     

      if (NameNode != null)

        Log["Message"](NameNode["text"]);

      if (ValueNode != null)

        Log["Message"](ValueNode["text"]);

     

    }


    Please let us know whether this information helps.

    Thank you.