<?xml version="1.0" encoding="gbk" ?> 
  <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
  <soapenv:Body>
  <ns3:MoSOAPRespBody xmlns:ns3="http://smses.zjseol.com.cn/esesms/">
  <ns3:message>
  <id>id</id> 
  <result>处理结果</result> 
  </ns3:message>
  </ns3:MoSOAPRespBody>
  </soapenv:Body>
  </soapenv:Envelope>怎么样取得id和result这两个节点的值?

解决方案 »

  1.   

        XmlDocument doc = new XmlDocument();
        doc.Load("xx.xml");    XmlNodeList elemList = doc.GetElementsByTagName("id");
        for (int i=0; i < elemList.Count; i++)
        {   
          Console.WriteLine(elemList[i].InnerXml);
        }
        elemList = doc.GetElementsByTagName("result");
        for (int i=0; i < elemList.Count; i++)
        {   
          Console.WriteLine(elemList[i].InnerXml);
        }   
      

  2.   

    <?xml version="1.0" encoding="gbk" ?> 
      <soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> 
      <soapenv:Body> 
      <ns3:MoSOAPRespBody xmlns:ns3="http://smses.zjseol.com.cn/esesms/"> 
      <ns3:message> 
      <id>id </id> 
      <result>处理结果 </result> 
      </ns3:message> 
      </ns3:MoSOAPRespBody> 
      </soapenv:Body> 
      </soapenv:Envelope> 是传递过来的一段字符串,不是一个xml文件
      

  3.   

    XML操作专题:http://www.bbs180.com/topictag-34.aspx读写等都有。
      

  4.   

    doc.load("xml文件字符串")提示错误:
    路径中具有非法字符。 
    ??
      

  5.   

     我是这么做的!不知道还有什么好的么! private void GetXmlData(string Path)
            {
                DataTable dt = new DataTable();            if (Path != string.Empty)
                {
                    DataSet ds = new DataSet();
                    try
                    {
                        ds.ReadXml(Path));
                        dt = ds.Tables["message"];                }
                    catch
                    {
                        throw new BusinessException("XML文件有问题,请导入正确的XML文件");
                    }
                                }        }得到的dt就是你要的id 和 result 的记录表!
      

  6.   


    XmlDocument xml = new XmlDocument();            string source = "<?xml version=\"1.0\" encoding=\"gbk\" ?>" +
                                            "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" " +
                                            "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"> " +
                                            "<soapenv:Body> " +
                                            "<ns3:MoSOAPRespBody xmlns:ns3=\"http://smses.zjseol.com.cn/esesms/\">" +
                                            "<ns3:message>" +
                                            "<id>id </id> " +
                                            "<result>处理结果 </result> " +
                                            "</ns3:message> " +
                                            "</ns3:MoSOAPRespBody> " +
                                            "</soapenv:Body> " +
                                            "</soapenv:Envelope> ";            byte[] bytes = Encoding.Default.GetBytes(source);
                MemoryStream ms = new MemoryStream(bytes, 0, bytes.Length);
                xml.Load(ms);
    这是把你的字符串转成XmlDocument的...
    然后你再从xmlDocument里取值就成了
      

  7.   

    try:            string str = @"<?xml version=""1.0"" encoding=""gbk"" ?>
      <soapenv:Envelope xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"">
      <soapenv:Body>
      <ns3:MoSOAPRespBody xmlns:ns3=""http://smses.zjseol.com.cn/esesms/"">
      <ns3:message>
      <id>id </id>
      <result>处理结果 </result>
      </ns3:message>
      </ns3:MoSOAPRespBody>
      </soapenv:Body>
      </soapenv:Envelope> ";
                XmlDocument doc = new XmlDocument();
                doc.LoadXml(str);
                foreach (XmlNode node in doc.GetElementsByTagName("id"))
                    Console.WriteLine(node.InnerXml);
                foreach (XmlNode node in doc.GetElementsByTagName("result"))
                    Console.WriteLine(node.InnerXml);
      

  8.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;namespace WindowsApplication5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();            System.Xml.NameTable NT = new System.Xml.NameTable();
                XmlNamespaceManager XNM = new XmlNamespaceManager(NT);
                XNM.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
                XNM.AddNamespace("ns3", "http://smses.zjseol.com.cn/esesms/");
                XmlDocument Doc = new XmlDocument();
                Doc.Load(@"c:\1.xml");
                XmlNode Node = Doc.SelectSingleNode("/soapenv:Envelope/soapenv:Body/ns3:MoSOAPRespBody/ns3:message/id", XNM);
                MessageBox.Show("id=" + Node.InnerText);
                Node = Doc.SelectSingleNode("/soapenv:Envelope/soapenv:Body/ns3:MoSOAPRespBody/ns3:message/result", XNM);
                MessageBox.Show("result=" + Node.InnerText);
            }
        }
    }
      

  9.   

    使用XPATH应该比较简单吧
     XmlDocument xml = new XmlDocument();
      xml.Load(Server.MapPath(Xml的路径);
      XmlNode xmlnode=xml.SelectSingleNode("id");
      string value=xmlnode.InnerText;
    这样就得到你想要的值了,当然也可以使用linq
      

  10.   

        XmlDocument doc = new XmlDocument();
        doc.Load("xx.xml");    XmlNodeList elemList = doc.DocumentElement.GetElementsByTagName("id");
        for (int i=0; i < elemList.Count; i++)
        {   
          Console.WriteLine(elemList[i].InnerXml);
        }
        elemList = doc.DocumentElement.GetElementsByTagName("result");
        for (int i=0; i < elemList.Count; i++)
        {   
          Console.WriteLine(elemList[i].InnerXml);
        }