1priyanka
14 years agoOccasional Contributor
How to read XML in C# scripting usingDOTnet 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
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