Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<Menus>

 <RootMenu id="STE" name="Stock Enquiry">

  <SubMenu id="ITM" name="Item Enquiry" form="ItemEnquiryForm" />
  <SubMenu id="LOC" name="Location Enquiry" form="LocationEnquiryForm" />
  <SubMenu id="LOT" name="Lot Enquiry" form="LotEnquiryForm" />
  <SubMenu id="PKG" name="Package Enquiry" form="PackageEnquiryForm" />
 </RootMenu>

 <RootMenu id="DIS" name="Dispatch">
  <SubMenu id="PPK" name="Piak and Pack" form="PickAndPackForm" />
   <SubMenu id="PUT" name="2 Steps Putaway" form="PutAwayForm" />

 </RootMenu>

  </Menus>


hi,all

can anyone tell me how can i retrieve the subMenu's name values from which the RootMenu's id=STE. i would like to get a list<string> for all the submenu's name values which belong to RootMenu with id=STE.

thanks
Posted
Updated 18-Jun-13 21:08pm
v2

You can also use linkQ to XML. Below is code for reference
XElement xelement = XElement.Load(@"C:\Menu.xml");
           IEnumerable<XElement> menus = xelement.Elements();
           List<string> subMenuList = new List<string>();
           foreach (var menu in menus)
           {
               if (menu.Attribute("id").Value == "STE")
               {
                   foreach (var submenu in menu.Elements())
                   {
                       subMenuList.Add(submenu.Attribute("name").Value);
                   }
               }
           }
 
Share this answer
 
You can also use XPath within the Xml DOM like this :
C#
string name;
XmlDocument xml = new XmlDocument();
xml.Load("theFile.xml"); 
// Or any other method to load your xml data in the XmlDocument.
// For example if your xml data are in a string, use the LoadXml method.
XmlElement elt = xml.SelectSingleNode("//SubMenu[@id='STE']") as XmlElement;
if(elt!=null)
{
  name=elt.GetAttribute("name");  
}


XPath/XQuery syntax can be found on internet: XPath Syntax (See the Selecting Nodes chapter)

If you need to retrieve a list of elements, you can use the SelectNodes method (it returns an XmlNodeList object).

C#
XmlNodeList submenus = xml.SelectNodes("//RootMenu[@id='STE']/SubMenu");
List<string> names = new List<string>();
foreach(XmlNode n in submenus)
{
  if(n is XmlElement)
    names.Add((n as XmlElement).GetAttribute("name"));
}
 
Share this answer
 
v3
There are a number of tools for managing XML in .Net.

XmlDocument
XmlReader

The following uses an XmlReader to find a specific element and get an attribute value.

C#
string myXml = "<test><a i=\"1\"/><a i=\"2\"/></a></a></test>";
System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(myXml));
System.Xml.XmlReader xr = System.Xml.XmlReader.Create(ms);

int firstIValue = 0;

while (xr.Read())
{
  if(xr.NodeType == System.Xml.XmlNodeType.Element)
    if (xr.Name == "a")
    {
      firstIValue = Convert.ToInt32(xr["i"]);
      break;
    }
}
 
Share this answer
 
v2
Comments
codingStar 19-Jun-13 3:39am    
hi,stephen

thanks for you answer. But in your code, how can i control if i want to just return the name value from <submenu name=""> which belongs to the RootMenu which ID=STE. I would like to get a list. And i don't want the touch this part in the first place
<rootmenu id="DIS" name="Dispatch">
<submenu id="PPK" name="Piak and Pack" form="PickAndPackForm">
<submenu id="PUT" name="2 Steps Putaway" form="PutAwayForm">


thanks

peter
codingStar 19-Jun-13 3:41am    
<rootmenu id="DIS" name="Dispatch">
<submenu id="PPK" name="Piak and Pack" form="PickAndPackForm">
<submenu id="PUT" name="2 Steps Putaway" form="PutAwayForm">



i don't want to touch this part at first. i just want to get the submenu name value for the first rootmenu which id=STE
Stephen Hewison 19-Jun-13 3:41am    
You change the check against Name to "RootMenu" and the reference to the attribute from "i" to "name"
codingStar 19-Jun-13 3:45am    
hi,stephen,

could you let me know how can i check against Name to RootMenu's ID value please

thanks a lot
Stephen Hewison 19-Jun-13 3:50am    
When the XmlReader is at the point of any specific XML node from your document. You can access the value for any attribute using the default collection on the xml reader. see the code xr["i"] just replace the "i" with any attribute name. Actually stop, look at the example and work out what it's doing. Then next time you won't have to ask the question.
C#
if (node.Attributes != null)
{
   var nameAttribute = node.Attributes["Name"];
   if (nameAttribute != null)
      return nameAttribute.Value;

   throw new InvalidOperationException("Node 'Name' not found.");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900