<?xml version="1.0" encoding="utf-8" ?> 
- <SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
- <SOAP-ENV:Header>
  <TransactionID xmlns="http://www.monternet.com/dsmp/schemas/">00110300006690</TransactionID> 
  </SOAP-ENV:Header>
- <SOAP-ENV:Body>
- <SyncOrderRelationReq xmlns="http://www.monternet.com/dsmp/schemas/">
  <Version>1.5.0</Version> 
  <MsgType>SyncOrderRelationReq</MsgType> 
- <Send_Address>
  <DeviceType>0</DeviceType> 
  <DeviceID>0011</DeviceID> 
  </Send_Address>
- <Dest_Address>
  <DeviceType>400</DeviceType> 
  <DeviceID>0</DeviceID> 
  </Dest_Address>
- <FeeUser_ID>
  <UserIDType>2</UserIDType> 
  <MSISDN /> 
  <PseudoCode>00110000000049</PseudoCode> 
  </FeeUser_ID>
- <DestUser_ID>
  <UserIDType>2</UserIDType> 
  <MSISDN /> 
  <PseudoCode>00110000000049</PseudoCode> 
  </DestUser_ID>
  <LinkID>SP</LinkID> 
  <ActionID>1</ActionID> 
  <ActionReasonID>1</ActionReasonID> 
  <SPID>900047</SPID> 
  <SPServiceID>0000000030</SPServiceID> 
  <AccessMode>2</AccessMode> 
  <FeatureStr>IA==</FeatureStr> 
  </SyncOrderRelationReq>
  </SOAP-ENV:Body>
  </SOAP-ENV:Envelope>
现在需要取得的是
  <LinkID>SP</LinkID> 
  <ActionID>1</ActionID> 
  <ActionReasonID>1</ActionReasonID> 
  <SPID>900047</SPID> 
  <SPServiceID>0000000030</SPServiceID> 
  <AccessMode>2</AccessMode>
这里面的数据
LinkID,ActionReasonID,SPID
怎么获得那??

解决方案 »

  1.   

    XmlDocument xmldoc = new XmlDocument();
    xmldoc.Load(...);
    string ns = "http://schemas.xmlsoap.org/soap/envelope/";
    XmlNode envelope = xmldoc["Envelope",ns];
    XmlNode body = envelope["Body",ns];
    XmlNode req = body["SyncOrderRelationReq"];
    XmlNode linkId = req["LinkID"];
    Console.WriteLine(linkId.InnerXml);
      

  2.   

    Private Function GetXmlSetting( _
            ByVal strFileName As String, _
            ByVal strSection As String, _
            ByVal strKey As String _
        ) As String
            Try
                Dim strReturn As String
                strReturn = ""            Dim objXmlDocument As XmlDocument
                objXmlDocument = New XmlDocument
                objXmlDocument.Load(strFileName)            Dim objNodeList As XmlNodeList
                objNodeList = objXmlDocument.GetElementsByTagName(strSection)
                If Not objNodeList Is Nothing Then
                    Dim childNodeList As XmlNodeList
                    childNodeList = objNodeList.Item(0).ChildNodes
                    If Not childNodeList Is Nothing Then
                        Dim objNode As XmlNode
                        For Each objNode In childNodeList
                            If objNode.Name = strKey Then
                                strReturn = objNode.InnerText
                                Exit For
                            End If
                        Next
                    End If
                End If            Return strReturn
            Catch ex As Exception
                Return ""
            End Try
        End Function------------------------------
    用这个函数传递文件名称,节点,子节点。
      

  3.   

    别用递归的办法用相对定位的办法好
    string ns = "http://schemas.xmlsoap.org/soap/envelope/";
    XmlNode envelope = xmldoc["Envelope",ns];
    XmlNode body = envelope["Body",ns];
    XmlNode req = body["SyncOrderRelationReq"];
    XmlNode linkId = req["LinkID"];
    Console.WriteLine(linkId.InnerXml);
      

  4.   

    如果知道确定的节点路径,最好用xpath,如果不知到是否存在这个节点就用递归。你的这个情形,应该先考虑用xpath查找.
      

  5.   

    XPath的方法确实更“标准”
    但是我从性能上考虑,除了使用XmlTextReader,这里为了方便使用XmlDocument,那么它一旦Load完毕,其DOM就已经建立
    此时如果再用XPath查询,那么还得动态解析XPath,然后再从DOM上提取数据
    还不如直接在已经建立好的DOM中用索引查找
      

  6.   

    楼上的各位,方法都试过了,都不错~从效果来看我还是同意 Sunmast(速马/Truly Madly Deeply的~不错~可惜本人XML没有学好啊,要恶补一下了
      

  7.   

    偶认为,用Xpath的方法还是比较好的.
    毕竟它更贴近OOP概念,使用时也更容易理解一些.
      

  8.   

    我的测试程序:
    class MainClass
    {
    static XmlDocument XmlDoc;
    static XmlNamespaceManager NM;
    static string NS = "http://schemas.xmlsoap.org/soap/envelope/"; [STAThread]
    static void Main(string[] args)
    {
    XmlDoc = new XmlDocument();
    XmlDoc.Load("C:/test.xml");
    NM = new XmlNamespaceManager(XmlDoc.NameTable);
    NM.AddNamespace("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
    NM.AddNamespace("Req", "http://www.monternet.com/dsmp/schemas/"); DateTime dt1,dt2;
    dt1 = DateTime.Now;
    string linkId;
    for(int i = 0;i < 1000000;i++)
    {
    linkId = XPathTest();
    }
    dt2 = DateTime.Now;
    Console.WriteLine("XPathTest: {0}",dt2 - dt1); dt1 = DateTime.Now;
    for(int i = 0;i < 1000000;i++)
    {
    linkId = DOMTest();
    }
    dt2 = DateTime.Now;
    Console.WriteLine("DOMTest: {0}",dt2 - dt1); Console.ReadLine();
    } static string XPathTest()
    {
    XmlNode linkId = XmlDoc.SelectSingleNode("/SOAP-ENV:Envelope/SOAP-ENV:Body/Req:SyncOrderRelationReq/Req:LinkID", NM);
    return linkId.InnerXml;
    } static string DOMTest()
    {
    XmlNode linkId = XmlDoc["Envelope",NS]["Body",NS]["SyncOrderRelationReq"]["LinkID"];
    return linkId.InnerXml;
    }
    }结果是:
    XPathTest: 00:00:27.4218750
    DOMTest: 00:00:03.8750000你可以看到性能差异非常的大
      

  9.   

    http://blog.sunmast.com/Sunmast/archive/2005/04/13/1716.aspx