2012-04-08 76 views
0

我有一個XML文件讀取多個值

<Actual-External-Terminals> 
    <Actual-External-Terminal> 
     <Party-Id value="EXTRA:77440" /> 
     <Name value="77440" /> 
     <Dial-String value="77440" /> 
     <IP-ISDN-SIP value="IP" /> 
     <Total-Connection-Time value="0s" /> 
     <Failing-Attempts value="0" /> 
     <Last-Failure-Cause value="N/A" /> 
     <List-of-Connection-Records> 
     <Connection Call-Rate="768" Call-Type="Video" ConnectionTime="" Dialin-Dialout="Dial-in" Disconnection-Time="2012-02-16T13:33:32Z" Over-GW-port-limit="false" Over-MCU-port-limit="false" Reason-Disconnection="Normal" /> 
     </List-of-Connection-Records> 
    </Actual-External-Terminal> 

在上面的XML我想連接段的所有值請幫我謝謝 不能使用LINQ到XML因爲它的.NET Framework 2.0和桌面應用程序

問候 wasif

+0

谷歌C#xpath – 2012-04-08 22:06:59

回答

1

System.Xml是一種方法。如下:

using System.Xml; 
// Name space & class declarations... 
static void ReadXml(string filePath) 
     { 
      //These would be kept in a settings file but constants for this example 
      const string CONNECTION_LISTING_NODE_NAME = "List-of-Connection-Records"; 
      const string CONNECTION_NODE_NAME = "Connection"; 
      const string CALL_RATE_ATTRIBUTE_NAME = "Call-Rate"; 

      //Load xml 
      var doc = new XmlDocument(); 
      doc.Load(filePath); 
      var root = doc.FirstChild; 
      var connectionRecordLists = doc.SelectNodes(String.Format("//{0}",CONNECTION_LISTING_NODE_NAME)); 
      if (connectionRecordLists == null) return; 
      for (var i = 0; i < connectionRecordLists.Count; i++) 
      { 
       var connections = connectionRecordLists[i].SelectNodes(CONNECTION_NODE_NAME); 
       if (connections == null) continue; 
       for (var j = 0; j < connections.Count; j++) 
       { 
        if (connections[j].Attributes != null 
         && connections[j].Attributes[CALL_RATE_ATTRIBUTE_NAME] != null) 
        { 
         Console.WriteLine(connections[j].Attributes[CALL_RATE_ATTRIBUTE_NAME].Value); 
        } 

       } 
      } 
     } 
+0

感謝GrantVS它的工作非常感謝:-) – Wasif 2012-04-09 05:58:36