Solved

programming through XMLdocument using C#

Posted on 2008-10-06
5
859 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
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 350 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 350 total points
ID: 22657288
use  xDoc.DocumentElement.ChildNodes insteed of xDoc.ChildNodes
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 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