Forum Discussion

anatar02's avatar
anatar02
Contributor
14 years ago

[C#] Getting [object Error]error while executing the second node of xml

[C#] Getting [object Error]error while executing the second node of xml



Hi All,



I
am using below setup of C# code to retrieving the values by node and
its working fine with a single node, if I am retrieving only one node in
a script, then its working well and fine but the same time its giving
[object Error] error message when I wanted to retrieve more than one
nodes a script. I have done the  research on the failure but I could not
find the root cast in this, so can someone please help me with this.

Sample sample is attached.



Code :-

function doGetxmlvaluebyNode(filepath, testdata)

{

  var Doc, s, Nodes, Node;

  

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

  Doc["async"] = false;

  Doc["load"](filepath);

 

  // Use the XPath expression to obtain the needed node

  NameNode = Doc["selectSingleNode"]("//TestData/Data[Name='"+testdata+"']/Value");   



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



 

  doGetxmlvaluebyNode=(ValueNode["text"])

    Doc[null];        

    return doGetxmlvaluebyNode;

}



Sample Xml



<TestData>

    <Data>

        <Name>executionMode</Name>

        <Value>debug</Value>

    </Data>

    <Data>

        <Name>Sub_Environment</Name>

        <Value>LastName</Value>

    </Data>

        <Data>

        <Name>Company</Name>

        <Value>test</Value>

    </Data>

    

</TestData>



Please help me on this.



//USEUNIT Utility





function test  ()

{

var filePath= "C:\\Data.xml";

var executionMode=Utility["doGetxmlvaluebyNode"](filePath,"executionMode");



Log["Message"](executionMode);



Note :-



Till here its working fine and its retrieving the expected node successfully.  its throwing an error below code


var Sub_Environment=Utility["doGetxmlvaluebyNode"](filePath,"Sub_Environment");

Log["Message"](Sub_Environment);



}
  • Hi Ashok,



    The problem is the same as the one discussed in the following thread:

    first call to function is working but second isn't



    Please see my reply there for explanations why this happens.





    To fix your script, you need to change the following code:

      ...

      doGetxmlvaluebyNode=(ValueNode["text"])

      Doc[null];

      return doGetxmlvaluebyNode;

    }
    to this:

      ...

      var value = ValueNode["text"];

      Doc = null;

      return value;

    }


    Also, note that to "nullify" variables, you need to use Doc = null, not Doc[null].
  • Hey Helen ,



    Thanks for your reply. after the long re-search on the issues, i have found the root cause of the failure that was, due to the Method  name and return variable was same. so i have re-defined the return  variable and its working well and fine now.

    Thanks

    Ashok N