当前位置: 代码迷 >> XML/SOAP >> ,关于XML里面元素的提取,用C
  详细解决方案

,关于XML里面元素的提取,用C

热度:322   发布时间:2012-02-19 19:43:39.0
求救,关于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都取出来,这个该怎么样操作啊? 谢谢了啊,我想要具体的代码。

------解决方案--------------------
C# code

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;
    } 
  相关解决方案