Solved

programming through XMLdocument using C#

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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 document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

726 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