?
Solved

programming through XMLdocument using C#

Posted on 2008-10-06
5
Medium Priority
?
862 Views
Last Modified: 2013-12-17
Hi all,

I want to read all the nodes under the node <Lists> and get the attribute Value and load in the list box
I  have code like this ....

But the loop read the arrtibute only in <Lists>  and exit the loop? Please correct me whr i went wrong...
<Lists>
<List DocTemplateUrl="1" Name="Go1" Value ="Hello" />
<List DocTemplateUrl="2" Name="Go2" Value ="Fun" />
<List DocTemplateUrl="3" Name="Go3" Value ="Load" />
</Lists>
 
 
XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(((System.Xml.XmlElement)(args1)).OuterXml);
 
            foreach (XmlNode xoData in xDoc.ChildNodes)
            {
                try
                {
                    XmlElement tempxmlEle = (XmlElement)xoData;
                    ddl_List.Items.Add(tempxmlEle.GetAttribute("Value").ToString());
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString());
                }
            }

Open in new window

0
Comment
Question by:Vivek Thangaswamy
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 14

Expert Comment

by:rachitkohli
ID: 22657028
Instead of
foreach (XmlNode xoData in xDoc.ChildNodes)

Try
foreach (XmlNode xoData in xDoc.selectnodes("//Lists")
or
foreach (XmlNode xoData in xDoc.selectnodes("Lists")
0
 
LVL 14

Accepted Solution

by:
Binuth earned 1050 total points
ID: 22657051
try this
	System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
	xDoc.LoadXml(@"<Lists><List DocTemplateUrl='1' Name='Go1' Value ='Hello' /><List DocTemplateUrl='2' Name='Go2' Value ='Fun' /><List DocTemplateUrl='3' Name='Go3' Value ='Load' /></Lists>");
 
 
	foreach (System.Xml.XmlElement xElm in xDoc.DocumentElement.ChildNodes )
	{
		if (xElm.Name.ToLower() == "list")
		{
			//xElm.Attributes["DocTemplateUrl"].Value
			//xElm.Attributes["Name"].Value
			//xElm.Attributes["Value"].Value
		}
	}

Open in new window

0
 
LVL 11

Author Comment

by:Vivek Thangaswamy
ID: 22657104

ddl_List.Items.Add(
xElm.GetAttribute("Title").ToString());
Iam i doing it correctly to add the value to the List control...
0
 
LVL 6

Expert Comment

by:avnish_tanna
ID: 22657167
Try below code:
This code reads the data from xml file.
' Open an XML file
Dim reader As New XmlTextReader(Server.MapPath("../xml/Lists.xml"))
While reader.Read()
If reader.Name.ToString() = "List" Then
 Response.Write(reader.Name.ToString() & ":" & reader("Value") & "<br>")
End If
End While

Open in new window

0
 
LVL 14

Assisted Solution

by:Binuth
Binuth earned 1050 total points
ID: 22657288
use  xDoc.DocumentElement.ChildNodes insteed of xDoc.ChildNodes
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question