Solved

programming through XMLdocument using C#

Posted on 2008-10-06
5
857 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
Comment Utility
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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility
use  xDoc.DocumentElement.ChildNodes insteed of xDoc.ChildNodes
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now