[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

programming through XMLdocument using C#

Posted on 2008-10-06
5
Medium Priority
?
865 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

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

649 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