求救,关于XML里面元素的提取,用C#
这个是我的XML文件代码:
<?xml version="1.0" encoding="utf-8"?>
<ConversationTransitions>
<Transition>
<SourceInteraction href="OP1"/>
</Transition>
<Transition>
<SourceInteraction href="OP2"/>
</Transition>
<Transition>
<SourceInteraction href="OP3"/>
</Transition>
<Transition>
<SourceInteraction href="OP4"/>
</Transition>
</ConversationTransitions>
我想把OP1,OP2,OP3,OP4都取出来,这个该怎么样操作啊? 谢谢了啊,我想要具体的代码。
[解决办法]
public void MainOption() { string xmlFilePath = @"xmlfilePath.xml"; //filePath List<string> opList = new List<string>(); opList = GetOPValues(xmlFilePath); } private List<string> GetOPValues(string xmlFilePath) { List<string> opList = new List<string>(); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xmlFilePath); XmlElement xmlRoot = xmlDoc.DocumentElement; XmlNodeList nodeList = xmlDoc.SelectNodes("ConversationTransitions/Transition/SourceInteraction"); foreach (XmlNode node in nodeList) { opList.Add(node.Attributes["href"].Value); } return opList; }