I am trying to write a samll test script on the following xml file. I have a node <Type> which has different values. How can I read
<Type> node name and its values to perform different operations based on the values? I need your help. Thank you in advance
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
void edit()
{
Response.Write("please edit the following fields");
Dim xmlDoc As New XmlDocument
dim X as integer
dim I as integer
Dim xmlIdentityCode As XmlNodeList
Dim xmlTypeDetail As XmlNodeList
dim sType as String
For I = 0 To xmlIdentityCode.Count - 1
If xmlIdentityCode.Item(I).ChildNodes.Item(0).Name ="Type" then
sType =xmlIdentityCode.Item(I).InnerText.ToString
End If
Next
***ChildNodes.Item(0) -- you can also check the count of childnodes and tru loop can retrive value.
Mark as Answer, on the posts reply's that helped you !!!.
mesfinay2002
0 Points
10 Posts
how to read XML node.Name and node.Value
Nov 25, 2007 09:32 AM|LINK
Hi experts!
I am trying to write a samll test script on the following xml file. I have a node <Type> which has different values. How can I read <Type> node name and its values to perform different operations based on the values? I need your help. Thank you in advance
<?xml version="1.0" ?>
<List>
<Identity>
<Type>500</Type>
<Number>11111</Number>
<Name> Sara</Name>
</Identity>
<Order>
<OrderID>2000</OrderID>
<Nr>100</Nr>
<Name>XXXX YYYYY</Name>
</Order>
<Identity>
<Type>501</Type>
<Number>21111</Number>
<Name> cccc</Name>
</Identity>
<Order>
<OrderID>9000</OrderID>
<Nr>100</Nr>
<Name>bbbb</Name>
</Order>
<Identity>
<Type>500</>
<Number>30000</Number>
<Name>Jimmy</Name>
</Identity>
<Order>
<OrderID>3</OrderID>
<Nr>200</Nr>
<customerName>zzzzz</customerName>
</Order>
<Identity>
<Type>501</Type>
<Number>30000</Number>
<Name>xvxvxv </Name>
</Identity>
<Order>
<OrderID>6000</OrderID>
<Nr>400</Nr>
<customerName>hhhh</customerName>
</Order>
</List>
The following code gives me null value:
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Xml" %>
<script runat="server">
void Page_Load(Object sender , EventArgs e)
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(Server.MapPath("test.xml"));
XmlNode rootnode = xmldoc.DocumentElement;
checkNode(rootnode);
}
void checkNode(XmlNode node)
{
//get the NodeType ,node name and its value
if (node.NodeType == XmlNodeType.Element) {
if ((node.Name == "Type" ) && (node.Value.ToString() == "500")){
perform();
}
else if ((node.Name == "Type") && (node.Value.ToString() == "501" ))
{
edit();
}
else
{
Response.Write("xxxxxxx Type could not be found");
}
}
}
void perform()
{
System.Data.DataSet ds;
ds = new System.Data.DataSet();
ds.ReadXml(Server.MapPath(@"test.xml"));
DataGrid1.DataSource = ds.Tables["Identity"];
DataGrid1.DataBind();
lblTable1.Text = ds.Tables["Identity" ].TableName;
lblTable2.Text = ds.Tables["Order" ].TableName;
DataGrid2.DataSource = ds.Tables["Order"];
DataGrid2.DataBind();
}
public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */
}
void edit()
{
Response.Write("please edit the following fields");
}
</Script>
<html>
<head><title>Read.aspx</title></head>
<body>
<asp:Label
ID="lblTable1"
Font-Size="14pt"
Runat="Server"/>
<br>
<asp:GridView
ID="DataGrid1"
CellPadding="10"
Runat="Server" />
<br>
<asp:Label
ID="lblTable2"
Font-Size="14pt"
Runat="Server"/>
<br>
<asp:GridView
ID="DataGrid2"
CellPadding="10"
Runat="Server" />
</body>
</html>
The above code gives me "Type could not found.... " I am new to Xml please help me. I
Haissam
All-Star
37421 Points
5632 Posts
Re: how to read XML node.Name and node.Value
Nov 25, 2007 09:50 PM|LINK
below is a sample code
XmlNode parentNode = xmlDoc.SelectNodes("List/Identity");
if(parentNode.ChildNodes[0].name=="Type")
{
switch(parentNode.ChildNodes[0].value)
{
case "500":
// write your code
break;
}
}
HC
MCAD.NET
| Blog |
ksridharbabu...
Star
9239 Points
1356 Posts
Re: how to read XML node.Name and node.Value
Nov 26, 2007 04:44 AM|LINK
The issue in your code is that you are passing an XMLNode to the checkNode function. And this XMLNode is of DocumentElement.
And in the code you are checking directly the elementName and value for the "Type". We can change the checkNode function as follow:
XmlNode objNode = node.selectSingleNode("Identity/Type");
if(objNode != null){
if (objNode.Value.ToString() == "500"){
perform();
}
else if (objNode.Value.ToString() == "501" )
{
edit();
}
}
Visit My Blog
-------------------------------------------------
If this post was useful to you, please mark it as answer. Thank you!
mesfinay2002
0 Points
10 Posts
Re: how to read XML node.Name and node.Value
Nov 26, 2007 08:19 AM|LINK
Hi Haissam,
Thank you for your reply.
I have tried your suggestion. It throws an error: Cannot implicitly convert type 'System.Xml.XmlNodeList' to 'System.Xml.XmlNode'
XmlNode parentNode = node.SelectNodes("/List/Type");
please help me!
mesfinay2002
0 Points
10 Posts
Re: how to read XML node.Name and node.Value
Nov 26, 2007 08:21 AM|LINK
Hi Ksridharbabuus,
Thank you for your reply.
I have tried your suggestion. It displays an empty page. ! Please help me
suthish nair
All-Star
15176 Points
3304 Posts
Re: how to read XML node.Name and node.Value
Nov 26, 2007 08:40 AM|LINK
Dim xmlDoc As New XmlDocument
dim X as integer
dim I as integer
Dim xmlIdentityCode As XmlNodeList
Dim xmlTypeDetail As XmlNodeList
dim sType as String
xmlDoc.Load(Server.MapPath("Response.xml"))
xmlIdentityCode = xmlDoc.GetElementsByTagName("List")(X).SelectNodes("Identity")
For I = 0 To xmlIdentityCode.Count - 1
If xmlIdentityCode.Item(I).ChildNodes.Item(0).Name ="Type" then
sType =xmlIdentityCode.Item(I).InnerText.ToString
End If
Next
***ChildNodes.Item(0) -- you can also check the count of childnodes and tru loop can retrive value.
mesfinay2002
0 Points
10 Posts
Re: how to read XML node.Name and node.Value
Nov 26, 2007 09:39 AM|LINK
Thank you for your reply,
where have used xmlTypeDetail?
Dim xmlIdentityCode As XmlNodeList throws an error. Cannot create an instance of the abstract class or interface 'System.Xml.XmlNodeList'
mes
mesfinay2002
0 Points
10 Posts
Re: how to read XML node.Name and node.Value
Nov 26, 2007 01:34 PM|LINK
i couldn't go further because of this error
any help is appreciable
mesfinay2002
0 Points
10 Posts
Re: how to read XML node.Name and node.Value
Nov 26, 2007 07:27 PM|LINK
It is OK! XmlNode + XmlNodeType+XmlNodeList+FirstChild have solved my problem
mes
suthish nair
All-Star
15176 Points
3304 Posts
Re: how to read XML node.Name and node.Value
Nov 27, 2007 07:30 AM|LINK
Mes,
I only just shown an logic example how to read a node.
Its upon u to work on it. :-)