I have a xml document in the above format. I have written code to dynamically bind the dropdownlists using the above xml. I have used xpath for querying the and below is the code for that
public SortedDictionary<string, string> GetDropdownContent(string ID)
{
/*Function to populate dropdown lists through xml */
SortedDictionary<string, string> sortedDropdownListContent = new SortedDictionary<string, string>();
XPathNavigator xPathNavigator = dropDownXMLDocument.CreateNavigator();
XPathNodeIterator xpathNodeIterator = xPathNavigator.Select("//DropDownLists//DropDownList[@ID='" + ID + "']");
XmlNamespaceManager xmlNameSpaceManager = new XmlNamespaceManager(xPathNavigator.NameTable);
if (xpathNodeIterator != null)
{
while (xpathNodeIterator.MoveNext())
{
sortedDropdownListContent.Add(
xpathNodeIterator.Current.GetAttribute("Text", xmlNameSpaceManager.DefaultNamespace),
xpathNodeIterator.Current.GetAttribute("Value", xmlNameSpaceManager.DefaultNamespace)
);
}
}
return sortedDropdownListContent;
}
The issue that i'm facing is that the below line is returning all the nodes in the xml document instead of returning only elements for the ID i have specified in the select statement.
XPathNodeIterator xpathNodeIterator = xPathNavigator.Select("//DropDownLists//DropDownList[@ID='" + ID + "']");
Kindly help me if i have missed anything in the code.
using System.Xml.Linq;
public SortedDictionary<string, string> GetDropdownContent(string ID)
{
/*Function to populate dropdown lists through xml */
XDocument dropDownXMLDocument = XDocument.Load(@"Ur Xml File path");
SortedDictionary<string, string> sortedDropdownListContent = new SortedDictionary<string, string>();
XPathNavigator xPathNavigator = dropDownXMLDocument.CreateNavigator();
var nodes = from node in dropDownXMLDocument.Descendants("DropDownList")
where node.Attribute("ID").Value == ID
select node;
XmlNamespaceManager xmlNameSpaceManager = new XmlNamespaceManager(xPathNavigator.NameTable);
if (nodes.Count() == 1)
{
foreach (XElement node in nodes.DescendantNodes())
{
sortedDropdownListContent.Add(node.Attribute("Text").Value, node.Attribute("Value").Value);
}
}
return sortedDropdownListContent;
}
manojrockz
Member
108 Points
86 Posts
Getting the xml nodes by ID using xpath
May 15, 2012 07:09 AM|LINK
<DropDownLists> <DropDownList ID="Marital"> <ListItem Text="- Select -" Value=""></ListItem> <ListItem Text="Married" Value="1"></ListItem> <ListItem Text="Unmarried (single/divorced/widowed)" Value="2"></ListItem> <ListItem Text="Separated" Value="3"></ListItem> </DropDownList> <DropDownList ID="Citizenship"> <ListItem Text="- Select -" Value=""></ListItem> <ListItem Text="US Citizen" Value="1"></ListItem> <ListItem Text="Canadian Citizen" Value="2"></ListItem> <ListItem Text="Permanent Resident Alien" Value="3"></ListItem> <ListItem Text="Neither US or Canadian Citizen" Value="4"></ListItem> </DropDownList> </DropDownLists>I have a xml document in the above format. I have written code to dynamically bind the dropdownlists using the above xml. I have used xpath for querying the and below is the code for that
public SortedDictionary<string, string> GetDropdownContent(string ID) { /*Function to populate dropdown lists through xml */ SortedDictionary<string, string> sortedDropdownListContent = new SortedDictionary<string, string>(); XPathNavigator xPathNavigator = dropDownXMLDocument.CreateNavigator(); XPathNodeIterator xpathNodeIterator = xPathNavigator.Select("//DropDownLists//DropDownList[@ID='" + ID + "']"); XmlNamespaceManager xmlNameSpaceManager = new XmlNamespaceManager(xPathNavigator.NameTable); if (xpathNodeIterator != null) { while (xpathNodeIterator.MoveNext()) { sortedDropdownListContent.Add( xpathNodeIterator.Current.GetAttribute("Text", xmlNameSpaceManager.DefaultNamespace), xpathNodeIterator.Current.GetAttribute("Value", xmlNameSpaceManager.DefaultNamespace) ); } } return sortedDropdownListContent; }The issue that i'm facing is that the below line is returning all the nodes in the xml document instead of returning only elements for the ID i have specified in the select statement.
XPathNodeIterator xpathNodeIterator = xPathNavigator.Select("//DropDownLists//DropDownList[@ID='" + ID + "']");Kindly help me if i have missed anything in the code.
Regards, manoj C S
xpath
ramanselva
Contributor
2064 Points
324 Posts
Re: Getting the xml nodes by ID using xpath
May 15, 2012 08:02 AM|LINK
Hi,
Below reference had a sample for your issue:
http://msdn.microsoft.com/en-us/library/0ea193ac.aspx
xpath
This can be beneficial to other community members reading the thread.
Regards,
Rama Selvam M.
Ramesh T
Contributor
5131 Points
827 Posts
Re: Getting the xml nodes by ID using xpath
May 15, 2012 08:52 AM|LINK
Fancy doing this in LINQ?
using System.Xml.Linq; public SortedDictionary<string, string> GetDropdownContent(string ID) { /*Function to populate dropdown lists through xml */ XDocument dropDownXMLDocument = XDocument.Load(@"Ur Xml File path"); SortedDictionary<string, string> sortedDropdownListContent = new SortedDictionary<string, string>(); XPathNavigator xPathNavigator = dropDownXMLDocument.CreateNavigator(); var nodes = from node in dropDownXMLDocument.Descendants("DropDownList") where node.Attribute("ID").Value == ID select node; XmlNamespaceManager xmlNameSpaceManager = new XmlNamespaceManager(xPathNavigator.NameTable); if (nodes.Count() == 1) { foreach (XElement node in nodes.DescendantNodes()) { sortedDropdownListContent.Add(node.Attribute("Text").Value, node.Attribute("Value").Value); } } return sortedDropdownListContent; }xpath
manojrockz
Member
108 Points
86 Posts
Re: Getting the xml nodes by ID using xpath
May 15, 2012 09:35 AM|LINK
Thanks for Your replies. We are working on asp.net 2.0 version and we do not have the linq in this framework.
Can you help ?
Regards,
Manoj CS
xpath
Ramesh T
Contributor
5131 Points
827 Posts
Re: Getting the xml nodes by ID using xpath
May 15, 2012 09:59 AM|LINK
Try this
public SortedDictionary<string, string> GetDropdownContent(string ID) { XmlDocument dropDownXMLDocument = new XmlDocument(); dropDownXMLDocument.Load(@"Ur XML file path"); SortedDictionary<string, string> sortedDropdownListContent = new SortedDictionary<string, string>(); XPathNavigator xPathNavigator = dropDownXMLDocument.CreateNavigator(); XmlNodeList xnList = dropDownXMLDocument.SelectNodes("//DropDownLists//DropDownList[@ID='" + ID + "']"); XmlNamespaceManager xmlNameSpaceManager = new XmlNamespaceManager(xPathNavigator.NameTable); if (xnList.Count == 1) { foreach (XmlNode node in xnList[0].ChildNodes) { sortedDropdownListContent.Add(node.Attributes["Text"].Value, node.Attributes["Value"].Value); } } return sortedDropdownListContent; }manojrockz
Member
108 Points
86 Posts
Re: Getting the xml nodes by ID using xpath
May 15, 2012 11:12 AM|LINK
Thanks Ramesh. It worked :)
But what you have given is not xpath right ?
Regards,
Manoj C S