本文介绍了如何遍历以下XML文件并打印节点的子元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何遍历以下XML文件并打印DaysOfWeek节点的子元素节点名称?我只需要周二和周三作为输出。请建议。



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.

<ScheduleInfo>

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

</ScheduleInfo>

推荐答案


using System.Xml.Linq;
using System.Xml;



C#代码


C# code

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


这篇关于如何遍历以下XML文件并打印节点的子元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 17:24