Forum Discussion

1priyanka's avatar
1priyanka
Occasional Contributor
14 years ago

How to read XML in C# scripting using DOTnet classes??

hello,



I want to read an XML using Dotnet classes for which i have already added the System_XML and System_XML_Linq

assemblies in CLR bridge.but I am facing some problems while running the
code and I am unable to relate the problem here i am sending you the
piece of code which i wrote and the XML for same kindly look into it.
and help me in effiently reading it.

function Class1()

{

  var ws=0

  var pi=0;

  var dc=0;

  var cc=0;

  var ac=0;

  var et=0;

  var el=0;

  var xd=0;

 

  //Read a doc

 

  textReader=dotNET["System_Xml"]["XmlTextReader"]["zctor_11"]("C:\\Documents and Settings\\priyankS\\Desktop\\KBT.xml");  

  //Read until end of file

 

  while(textReader["Read"]())

  {

    var nType=textReader["NodeType"];

    

    //if node type use a declaration

    if(nType==dotNET["System_Xml"]["XmlNodeType"]["XmlDeclaration"])

    {

      Log["Message"]("Declaration:"+textReader["Name"])

      Log["Message"]("Hi")

      xd=xd+1;

    }

    //if node type is comment

    if(nType==dotNET["System_Xml"]["XmlNodeType"]["Comment"])

    {

      Log["Message"]("Comment:"+textReader["Name"]);

      cc=cc+1;

    }

    //if node type is an attribute

    if(nType==dotNET["System_Xml"]["XmlNodeType"]["Attribute"])

    {

      Log["Message"]("Attribute:"+textReader["Name"]);

      ac=ac+1;

    }

     

    

   }

    

 

}



//The XML which I am using here is

//****************************************************************************

<?xml version="1.0" encoding="utf-8"?>

<controls>

     <control Class="Button"  Name0="Copy" Name1="Add" Name2="Save"
Name3="Cancel" Name4="Delete" Name5="AutoReg"
Name6="check"></control>

     <control Class="ListBox" ></control>

     <control Class="RadioButton" Name1="Show All"  Name2="Selected"  Name3="Not Used"></control>

     <control Class="Label" Name1="Name"></control>

     <control Class="TextBox"></control>

     <control Class="VScrollBar"></control>

     <control Class="DataGridViewTextBoxColumn"  Name0="CheckPoint
Tag" Name1="CheckPoint Name" Name2="Expected" Name3="MaxBefore"
Name4="MaxAfter" ></control>

     <control Class="DataGridViewCheckBoxColumn" Name1="Include"></control>  

</controls>



//****************************************************************************************************
the
above code does not throws any error on execution but it neither give
any output even though it satisfies the condition ,control does not goes
inside the if loop and log the message



and the another piece of code on which I am trying is

function Class2()

   {     

        

        

        var xdXmlFile =
dotNET["System_Xml_Linq"]["XDocument"]["Load_2"]("C:\\Documents and
Settings\\priyankS\\Desktop\\KBT.xml");

                    var xeRootNode = xdXmlFile["Root"];



                    var xeElementCollection = xeRootNode["Elements"]()GetEnumerator();

                    //xeElementCollection.Reset();

                    while (xeElementCollection.MoveNext())

                    {

                        var xeElement = xeElementCollection.Current;



                        var xeAttributCollection = xeElement.Attributes().GetEnumerator();

                        while (xeAttributCollection.MoveNext())

                        {

                            var xaAttribute = xeAttributCollection.Current;

                            var AttributeName = xaAttribute.Name.LocalName;

                            var AttributeValue = xaAttribute.Value;

                        }

                    }



}



the above code  gives sysntax error on execution

4 Replies

  • Hi Priyanka,


    >>>

    it neither give any output even though it satisfies the condition ,control does not goes inside the if loop and log the message

    <<<

    I guess you just have a node which does not meet any of the specified conditions in your document. You can add a Log.Message call in the loop (e.g. Log.Message(nType)) to check all the node types. If this suggestion does not help, I recommend that you set a breakpoint and debug your script (see the "Debugging Tests - Overview" help topic ( http://www.automatedqa.com/support/viewarticle.aspx?aid=11335 ) for more information).


    >>>

    the above code  gives sysntax error on execution

    <<<

    I believe the error is caused by the following line:

    var xeElementCollection = xeRootNode["Elements"]()GetEnumerator();


    You missed the "." character between "xeRootNode["Elements"]()" and "GetEnumerator();"

  • 1priyanka's avatar
    1priyanka
    Occasional Contributor
    Hello Allen,



    Thanks for response  n suggestion :-)

     as suggested by you here



    Hi Priyanka,


    >>>

    it neither give any output even though it satisfies the condition ,control does not goes inside the if loop and log the message

    <<<

    I guess you just have a node which does not meet any of the specified
    conditions in your document. You can add a Log.Message call in the loop
    (e.g. Log.Message(nType)) to check all the node types. If this
    suggestion does not help, I recommend that you set a breakpoint and
    debug your script (see the "Debugging Tests - Overview" help topic ( http://www.automatedqa.com/support/viewarticle.aspx?aid=11335 ) for more information).


    >>>



    actually   the problem is more intresting here as I already  tried the method above suggested by you  and yeah  I did debugging as well but my problem is in this chunk of code

    function Class1()

    {

      var ws=0

      var pi=0;

      var dc=0;

      var cc=0;

      var ac=0;

      var et=0;

      var el=0;

      var xd=0;

     

      //Read a doc

     

      textReader=dotNET["System_Xml"]["XmlTextReader"]["zctor_11"]("C:\\Documents and Settings\\priyankS\\Desktop\\KBT.xml");  

      //Read until end of file

     

      while(textReader["Read"]())

      {

        var nType=textReader["NodeType"];

        

        //if node type use a declaration

        if(nType==dotNET["System_Xml"]["XmlNodeType"]["XmlDeclaration"])

        {

          Log["Message"]("Declaration:"+textReader["Name"])

          Log["Message"]("Hi")

          xd=xd+1;

        }




    the value in" if(nType==dotNET["System_Xml"]["XmlNodeType"]["XmlDeclaration"])"

    both the side is same i.e" XmlDeclaration" type of object still the "==" equal to sign comparison gives false on debugging henceforth the control dont goes inside this loop.



    I don't know how come when both the side same value come on debugging then still it goes false.



    i request you to just run the  code once at yours end and see the debugger reponse.





    "XmlDeclaration" must satisfy the loop!





    and for other code reply which is:-

    <<<<

    the above code  gives sysntax error on execution

    <<<

    I believe the error is caused by the following line:

    var xeElementCollection = xeRootNode["Elements"]()

    GetEnumerator();


    You missed the "." character between "xeRootNode["Elements"]()" and "GetEnumerator();"


    <<<<<<<<

    I am doing here C# scripting and i dont think "." dot character will  solve the problem

    actually I did this coding in C# visual studio and ws trying to convert it into C# script ....

    so i asked for help in doing this



    anyways thnx again for help :-)

    Regards

    Priyanka




  • Hi Priyanka,




    both the side is same i.e" XmlDeclaration" type of object still the "==" equal to sign comparison gives false on debugging



    You're comparing two object references here, while you actually need to compare the values stored inside the objects. Here is the correct comparison line:



    nType["OleValue"] == dotNET["System_Xml"]["XmlNodeType"]["XmlDeclaration"]["OleValue"]




    I am doing here C# scripting and i dont think "." dot character will  solve the problem



    It doesn't matter which syntax you use, actually, unless you are going to use the code in a C# Connected application. C#Script is based on JScript, so you can use JScript syntax too. The same applies to C++Script. Anyway, here is the same line in C#Script syntax:



    var xeElementCollection = xeRootNode["Elements"]()["GetEnumerator"]();

  • 1priyanka's avatar
    1priyanka
    Occasional Contributor
    Heya!!!



    yeah  i  tried yours code  and it worked..my problem is solved  :))

    thanks alott Allen!



    Regards



    Priyanka