Solved

programming through XMLdocument using C#

Posted on 2008-10-06
5
858 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now