有一段XML,我想把其中一个节点下面的内容都都读到一个字符串里面,如下面的字符串,我想把
<WorkflowSpecification id="test"> 和 <WorkflowSpecification> 中的内容都读到一个字符串里面,可是程序写不对,帮忙看看。我的程序        private void readWorkflowKnowledgeBase(string path) {            XmlDocument doc = new XmlDocument();
            doc.Load(path);            XmlNodeList SpecNodeList = doc.GetElementsByTagName("WorkflowSpecification");
            foreach (XmlNode specNode in SpecNodeList) {
                string specid = specNode.Attributes["id"].InnerText;
                this.WorkflowKnowledgeBase.Add(specid, specNode.InnerText); // 感觉应该是InnerTest,可是怎么什么都没有呢
            }
            int a = 1;
        }<WorkflowSpecification id="test">
  <Workitem id="test1">  <!-- the id is here is the name of the workitem -->
    <Loaction type="fixed">
      <Point x="127" y="100" z="23"/>
    </Loaction>
    <Animation animationid = "123">
    </Animation>
  </Workitem>
  <Workitem id="test2">
    <Loaction type="fixed">
      <Point x="103" y="116" z="23"/>
    </Loaction>
    <Animation animationid = "123">
    </Animation>
  </Workitem>
  <Workitem id="test3">
    <Loaction type="fixed">
      <Point x="104" y="100" z="23"/>
    </Loaction>
    <Animation animationid = "123">
    </Animation>
  </Workitem>
</WorkflowSpecification>

解决方案 »

  1.   

    XmlTextReader reader = new XmlTextReader(filename); while (reader.Read())
     { if (reader.Name == "user1")
                                {
                                    string user1 = Convert.ToInt32(reader.ReadElementString());
                                 }
    }
      

  2.   

    想取那个节点也可以从document里选取
      

  3.   

    应该是这样把
    XmlNode root = xmlDoc.SelectSingleNode("WorkflowSpecification ");
    XmlNodeList xmlList = root.ChildNodes;
                        
      

  4.   

    XmlDocument doc = new XmlDocument();
    doc.Load(xmlPath);
    XmlNodeList nodes = doc.GetElementsByTagName("WorkflowSpecification");
    foreach (XmlNode node in nodes)
    {
        Console.WriteLine(node.Attributes["id"].Value);
        Console.WriteLine(node.InnerXml);
    }
      

  5.   

    using System.Xml;
    public partial class Xml读取方法_读取XML方法一 : System.Web.UI.Page
    {
        private void Page_Load(Object sender, EventArgs e)
        {
            XmlTextReader objXMLReader = new XmlTextReader(Server.MapPath("../XMLFiles/XMLFile.xml"));        string strNodeResult = string.Empty;
            XmlNodeType objNodeType;        while (objXMLReader.Read())
            {
                objNodeType = objXMLReader.NodeType;  //获取当前节点的类型            switch (objNodeType)
                {
                    case XmlNodeType.XmlDeclaration:   //Xml声明,读取XML文件头 
                        strNodeResult += "XML Declaration:<b>" + objXMLReader.Name + "" + objXMLReader.Value + "</b><br/>";
                        break;
                    case XmlNodeType.Element:   //元素,读取标签
                        strNodeResult += "Element:<b>" + objXMLReader.Name + "</b><br/>";
                        break;
                    case XmlNodeType.Text:    //节点的文本内容
                        strNodeResult += "-Value:<b>" + objXMLReader.Value + "</b><br/>";
                        break;
                }            if (objXMLReader.AttributeCount > 0)    //判断该节点是否有属性 
                {
                    /************ 循环判断完所有节点 ***********/
                    while (objXMLReader.MoveToNextAttribute())  //移动到下一个节点
                    {
                        strNodeResult += " -Attribute:<b>" + objXMLReader.Name + "</b> value:<b>" + objXMLReader.Value + "</b><br/>";
                    }
                }
                LblFile.Text = strNodeResult;
            }
        }
    }
    LblFile是前台页面的一个label控件,不知道这个对你是否有帮助