[RESOLVED] dropdownlist from xml
I have this loop:
Code:
foreach (XmlNode MyNodes in MyXmlDoc.SelectNodes("/Name"))
{
DDListClient.Items.Add(
new ListItem(MyNodes.ChildNodes[0].InnerText.ToString()));
}
and my xml is like below. I want to populate the "name" to my dropdownlist control, but it doesn't. What did i do wrong?
<Client>
<Name>test</Name>
<LastName>testing</LastName>
</Client>
Re: dropdownlist from xml
Your XPath is wrong. If youre XPath isn't wrong, then your XML is wrong.
Assuming the XML isn't wrong, and MyXmlDoc contains the entire XML file, then you should have multiple Client nodes under a root node. If you have multiple Client nodes, your Xpath should be used as
MyXmlDoc.SelectNodes("/Root/Client/Name")
Re: dropdownlist from xml
medhak, this is how I read the xml.. and my xml is right:
DataSet MyDS = new DataSet();
MyDS.ReadXml(Server.MapPath("~/Client.xml"));
XmlDataDocument MyXmlDoc = new XmlDataDocument(MyDS);
I tried the root node you suggested, but it's not working.
Re: dropdownlist from xml
oh I did a little bit more reserach this solves the issue:
XmlNode MyNodes in MyXmlDoc.SelectNodes("//Name")
But i don't know what // means?
Re: dropdownlist from xml
So the XML was right, but the XPath was wrong.
// does a recursive search across your XML and is very inefficient. You should be using the XPath to the node itself.
The
Code:
MyXmlDoc.SelectNodes("/Root/Client/Name")
method will work for you but I was simply guessing the name of the root node. You need to look at your XML (or show it to us) to figure out the proper path to the node you want.
Re: dropdownlist from xml
It simply looks like this for my xml:
<Client>
<Name>test</Name>
<LastName>testing</LastName>
</Client>
and it's under the project files, which is the same as the default.aspx... I didn't put it in any other folder. I simply right click on solution and add an xml file.
Re: dropdownlist from xml
So the XPath should be
/Client/Name
Re: [RESOLVED] dropdownlist from xml
Re: [RESOLVED] dropdownlist from xml