• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 867
  • Last Modified:

programming through XMLdocument using C#

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
Vivek Thangaswamy
Asked:
Vivek Thangaswamy
2 Solutions
 
rachitkohliCommented:
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
 
BinuthCommented:
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
 
Vivek ThangaswamyAuthor Commented:

ddl_List.Items.Add(
xElm.GetAttribute("Title").ToString());
Iam i doing it correctly to add the value to the List control...
0
 
avnish_tannaCommented:
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
 
BinuthCommented:
use  xDoc.DocumentElement.ChildNodes insteed of xDoc.ChildNodes
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now