<?xml version="1.0" encoding="utf-16"?>
<componentDocuments>
  <componentDocument Version="1.0">
    <header>
      <flow ID="GOGOGO" nodeID="Begin" >
        <fileName>xml/workflows/SignOnFlow.xml</fileName>
        <declares />
        <sequence outcome="" />
      </flow>
      <returnStack />
      <index>1</index>
    </header>
    <body>
      <userInfo persist="true">
        <InstitutionNo>0</InstitutionNo>
        <BranchNo>0</BranchNo>
      </userInfo>
      <screenData persist="true">
        <TranNo>100000</TranNo>
        <DynamicKey>adsfarfadfgdgzaf43sdtgrwafasf</DynamicKey>
      </screenData>
    </body>
  </componentDocument>
</componentDocuments>如何取得“fileName”元素下对应的值(即“xml/workflows/SignOnFlow.xml”)?
请大家帮忙解答一下,谢谢!

解决方案 »

  1.   

    通过XMLDocument加载文件,通过SelectSingleNode一层一层的找下去
      

  2.   

    string strContent = @"<?xml version=""1.0"" encoding=""utf-16""?> 
    <componentDocuments> 
      <componentDocument Version=""1.0""> 
        <header> 
          <flow ID=""GOGOGO"" nodeID=""Begin"" > 
            <fileName>xml/workflows/SignOnFlow.xml </fileName> 
            <declares /> 
            <sequence outcome="""" /> 
          </flow> 
          <returnStack /> 
          <index>1 </index> 
        </header> 
        <body> 
          <userInfo persist=""true""> 
            <InstitutionNo>0 </InstitutionNo> 
            <BranchNo>0 </BranchNo> 
          </userInfo> 
          <screenData persist=""true""> 
            <TranNo>100000 </TranNo> 
            <DynamicKey>adsfarfadfgdgzaf43sdtgrwafasf </DynamicKey> 
          </screenData> 
        </body> 
      </componentDocument> 
    </componentDocuments> ";            XmlDocument xml = new XmlDocument();
                xml.LoadXml(strContent);            XmlNode node =  xml.SelectSingleNode(@"//fileName");            string strValue = "";
                if (node != null) { strValue = node.InnerText; }
      

  3.   

    使用xpth,当然如果其他节点也有fileName节点,xpath就得写的详细点
      

  4.   


    XmlDocument xmldoc = new XmlDocument();
    xml = XElement.Load(xmlpath);
     XElement ele= xml.XPathSelectElement("componentDocuments/componentDocument/header/flow/fileName");
      

  5.   

    用xpath componentDocuments/componentDocument/header/flow/fileName
      

  6.   

    http://blog.csdn.net/sz101/archive/2010/01/11/5174212.aspx
      

  7.   

    对 就是通过xpath来查找节点的值
      

  8.   

    XmlDocument xml = new XmlDocument(); 
                xml.LoadXml(strContent);             XmlNode node =  xml.SelectSingleNode(@"//fileName");             string strValue = ""; 
                if (node != null) { strValue = node.InnerText; }
      

  9.   

    谢谢楼上各位的帮忙,刚才忘写了一个问题,就是取值出来后把值改变了然后再怎么保存到这个XML文件中呢?
    <?xml version="1.0" encoding="utf-16"?> 
    <componentDocuments> 
      <componentDocument Version="1.0"> 
        <header> 
          <flow ID="GOGOGO" nodeID="Begin" > 
            <fileName>xml/workflows/SignOnFlow.xml </fileName> 
            <declares /> 
            <sequence outcome="" /> 
          </flow> 
          <returnStack /> 
          <index>1 </index> 
        </header> 
        <body> 
          <userInfo persist="true"> 
            <InstitutionNo>0 </InstitutionNo> 
            <BranchNo>0 </BranchNo> 
          </userInfo> 
          <screenData persist="true"> 
            <TranNo>100000 </TranNo> 
            <DynamicKey>adsfarfadfgdgzaf43sdtgrwafasf </DynamicKey> 
          </screenData> 
        </body> 
      </componentDocument> 
    </componentDocuments> 
    <fileName>xml/workflows/SignOnFlow.xml </fileName>
    变为<fileName>c:\data\xml\workflows\SignOnFlow.xml </fileName>
    谢谢!
      

  10.   

    string xmlpath = @""; 
                XmlDocument doc = new XmlDocument(); 
                doc.Load(xmlpath); 
                XmlElement root = doc.DocumentElement; 
                XmlNode comnode = root.SelectSingleNode(""); 
                foreach (XmlNode n in comnode.ChildNodes) 
                { 
                    if (n.Attributes.GetNamedItem("").Value == "") 
                    { 
                        Console.WriteLine(n.InnerXml.ToString()); 
                    } 
                } 
                doc.Save(xmlpath);   
    http://topic.csdn.net/u/20090515/12/619c10d1-af12-4c90-bfcd-101da5dd8ddf.html 
      

  11.   

    谢谢“wuyq11”的解答,不过看来还是我没有说清楚:
    现在的情况是,报文是程序运行时生成出来的,但是出了点问题,我现在需要写一个方法修改这个报文中<fileName>配置节的信息,将其由“<fileName>xml/workflows/SignOnFlow.xml </fileName>”改为“<fileName>c:\data\xml\workflows\SignOnFlow.xml </fileName>”后返回一个新的报文,现在可以取到这个节点并修改了,但是修改后该怎样保存到先前的报文中取呢?
    请个人帮忙再解释解释吧,谢谢啦!