Click here to Skip to main content
15,881,898 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How can I traverse through the below XML file and print the child element node names of the DaysOfWeek node? I only need Tuesday and Wednesday as outputs. Please advice.

XML
<ScheduleInfo>

	<DaysOfWeek>
		<Tuesday>true</Tuesday>
		<Wednesday>true</Wednesday>
	</DaysOfWeek>

</ScheduleInfo>
Posted

 
Share this answer
 
Comments
mayooran99 23-Oct-14 11:52am    
I know XPath but i dont know how to use this in C# Please help.
PIEBALDconsult 23-Oct-14 11:59am    
I load the file into an XmlDocument and use SelectNodes, but others prefer XDocument and Linq. It's up to you. Have a look at those.
http://msdn.microsoft.com/en-us/library/system.xml.xmldocument(v=vs.110).aspx
http://msdn.microsoft.com/en-us/library/system.xml.linq.xdocument(v=vs.110).aspx
hi ,

plz try this code
namespaces needed
C#
using System.Xml.Linq;
using System.Xml;

C# code
C#
 string xml = @"<scheduleinfo>
 
	<daysofweek>
		<tuesday>true</tuesday>
		<wednesday>true</wednesday>
	</daysofweek>
 
</scheduleinfo>";
               XDocument xDoc = XDocument.Load(XmlReader.Create(new StringReader(xml)));
               var selectedElement = xDoc.Descendants("ScheduleInfo").Elements("DaysOfWeek");
               foreach (var element in selectedElement.Elements())
               {
                   //for value use element.Value.ToString()
                   //for Name use element.Name.ToString()
                   MessageBox.Show(element.Name.ToString());
               }


good luck ;-)
 
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