2011-05-17 69 views
1

我試圖選擇下面的XML的MINRANGE的內容。這是我正在使用的代碼,字符串min只是給我一個長文本塊,而不是我想要的節點。使用xpath與c#,幫助

XPathDocument _BTCall = new XPathDocument(callUrl); 
XPathNavigator nav = _BTCall.CreateNavigator(); 
XPathExpression exp; 
exp = nav.Compile("//MAX/MINRANGE"); 
XPathNodeIterator iterator = nav.Select(exp); 
iterator.MoveNext(); 

XPathNavigator nav2 = iterator.Current.Clone(); 
string min = nav.Value; 
return int.Parse(min); 

<ADSL_CHECKER> 
    <ERRORID>0</ERRORID> 
    <INPUT>01491410786</INPUT> 
    <INPUTTYPE>TELNO</INPUTTYPE> 
    <FIXEDRATE> 
    <RAG>G</RAG> 
    <READYDATE /> 
    <EXCHSTATE>E</EXCHSTATE> 
    <CAPACITY /> 
    </FIXEDRATE> 
    <RATEADAPTIVE> 
    <RAG>G</RAG> 
    <READYDATE /> 
    <EXCHSTATE>E</EXCHSTATE> 
    <CAPACITY /> 
    </RATEADAPTIVE> 
    <MAX> 
    <RAG>G</RAG> 
    <SPEED>4500</SPEED> 
    <MINRANGE>3500</MINRANGE> 
    <MAXRANGE>5500</MAXRANGE> 
    <READYDATE /> 
    <EXCHSTATE>E</EXCHSTATE> 
    <CAPACITY /> 
    </MAX> 
    <WBC> 
    <RAG>G</RAG> 
    <SPEED>5500</SPEED> 
    <MINRANGE>4500</MINRANGE> 
    <MAXRANGE>6500</MAXRANGE> 
    <READYDATE /> 
    <EXCHSTATE>E</EXCHSTATE> 
    <CAPACITY /> 
    </WBC> 
    <WBCFTTC> 
    <RAG>G</RAG> 
    <DOWNSPEED>32500</DOWNSPEED> 
    <UPSPEED>7200</UPSPEED> 
    <MINRANGE /> 
    <MAXRANGE /> 
    <READYDATE /> 
    <EXCHSTATE>E</EXCHSTATE> 
    <CAPACITY /> 
    </WBCFTTC> 
    <EXCHANGECODE>THHT</EXCHANGECODE> 
    <EXCHANGENAME>HENLEY-ON-THAMES</EXCHANGENAME> 
    <REASONCODE>L</REASONCODE> 
    <VPCONTENTION>N</VPCONTENTION> 
    <SPNAME /> 
    <CAD /> 
    <CPNAME>BE UN LIMITED</CPNAME> 
    <CPCONTACTNO>02074795000</CPCONTACTNO> 
    <POSTCODE>RG9 1LT</POSTCODE> 
    <SUGGESTEDMSG>Your exchange is ADSL enabled, and our initial test on your line indicates that your line should be able to have an ADSL broadband service that provides a fixed line speed up to 2Mbps. 

    Our test also indicates that your line currently supports an estimated ADSL Max broadband line speed of 4.5Mbps. Similar lines predicted with this speed have achieved ADSL Max line speeds in the range of 3.5 to 5.5Mbps. 
    Our test also indicates that your line currently supports an estimated ADSL2+ broadband line speed of 5.5Mbps. Similar lines predicted with this speed have achieved ADSL2+ line speed in the range of 4.5 to 6.5Mbps. 
    Our test also indicates that your line currently supports a fibre technology with an estimated WBC FTTC Broadband where consumers have received downstream line speed of 32.5Mbps and upstream line speed of 7.2Mbps. 
    The actual stable line speed supportable will be determined during the first 10 days of use. This speed may change over time, to ensure line stability is maintained. 
    If you decide to place an order, a further test will be performed to confirm if your line is suitable for the service you wish to purchase. 
    Thank you for your interest. 
    Please note that postcode and address check results are indicative only. Most accurate results can be obtained from a telephone number check. 
    </SUGGESTEDMSG> 
    <SUPPLEMENTARYMSG>Note: If you already have a Broadband service enabled on this line and you want to switch service providers, you will need to contact both your current provider and your new provider to get your service changed over new and existing service provider to have this service transferred. 
    </SUPPLEMENTARYMSG> 
</ADSL_CHECKER> 

在XML文本中,它們的目的略作修改。請參閱以前的版本。

回答

1

您需要獲取iterator.MoveNext()的結果。

XPathNodeIterator iterator = nav.Select(exp); 
if (iterator.MoveNext()) 
{ 
    XPathNavigator res = iterator.Current; 
    string min = res.Value; 
} 
else 
{ 
    //Error 
} 

iterator.MoveNext()不會修改原來的nav對象。

+0

但是MoveNext()返回一個bool而不是XPathNavigator? – 2011-05-17 13:54:40

+0

@Tom謝謝,需要迭代器的'Current'。 – 2011-05-17 13:59:04

2

您使用的是什麼版本的.Net框架?如果您使用的是3.5或更高版本,我強烈建議使用Linq來處理Xml,您將有更容易的時間。查看XDocument和相關的類。

http://msdn.microsoft.com/en-us/library/bb387044.aspx

+2

即時通訊使用2.0,如果我可以使用Linq它會很容易! – 2011-05-17 12:56:08

+1

@Tom Squires您可以使用LinqBridge(http://code.google.com/p/linqbridge/)在.NET 2.0中使用Linq。我不確定xml qureies,但對於sql工作正常。 – 2011-05-17 13:04:02

2

使用的XmlDocument實例和XML加載到其中,然後用的SelectNodes方法通過你的XPath查詢作爲輸入參數。

 XmlDocument xmlDocument = new XmlDocument(); 
     xmlDocument.LoadXml(xmlText); 
     var gg = xmlDocument.SelectNodes("//MAX/MINRANGE"); 

這會給你一個你可以遍歷的節點的集合。

0

實際上有不少選擇XmlNode內容的不同方法。讓我分享一些。 (當然你也可以修改代碼來使用XML,而不是裝載的字符串從XML文件)

使用XmlDocument對象和的SelectSingleNode()

//----------------------------------------------------------------------------- 
// <copyright file="Program.cs" company="DCOM Productions"> 
//  Copyright (c) DCOM Productions. All rights reserved. 
// </copyright> 
//----------------------------------------------------------------------------- 

namespace ConsoleApplication1 { 
    using System; 
    using System.Xml; 

    class Program { 
     static void Main(string[] args) { 
      XmlDocument xdoc = new XmlDocument(); 
      try { 
       xdoc.Load(".\\App.xml"); 
      } 
      catch (System.Xml.XmlException ex) { 
       // handle 
      } 
      XmlNode node = xdoc.SelectSingleNode("ADSL_CHECKER//MAX//MINRANGE"); 
      Console.WriteLine(node.InnerText); 
      Console.ReadKey(true); 
     } 
    } 
} 

使用一個XDocument對象和元素()

//----------------------------------------------------------------------------- 
// <copyright file="Program.cs" company="DCOM Productions"> 
//  Copyright (c) DCOM Productions. All rights reserved. 
// </copyright> 
//----------------------------------------------------------------------------- 

namespace ConsoleApplication1 { 
    using System; 
    using System.Xml; 
    using System.Xml.Linq; 

    class Program { 
     static void Main(string[] args) { 
      XDocument xdoc = XDocument.Load(".\\App.xml"); 
      XElement element = xdoc.Element("ADSL_CHECKER").Element("MAX").Element("MINRANGE"); 
      Console.WriteLine(element.Value); 
      Console.ReadKey(true); 
     } 
    } 
} 

使用一個XDocument對象和LINQ查詢

//----------------------------------------------------------------------------- 
// <copyright file="Program.cs" company="DCOM Productions"> 
//  Copyright (c) DCOM Productions. All rights reserved. 
// </copyright> 
//----------------------------------------------------------------------------- 

namespace ConsoleApplication1 { 
    using System; 
    using System.Linq; 
    using System.Xml; 
    using System.Xml.Linq; 

    class Program { 
     static void Main(string[] args) { 
      XDocument xdoc = XDocument.Load(".\\App.xml"); 
      var result = from e in xdoc.Element("ADSL_CHECKER").Element("MAX").Elements() 
         where e.Name == "MINRANGE" 
         select e; 
      Console.WriteLine(result.First().Value); 
      Console.ReadKey(true); 
     } 
    } 
} 

您的XPath的正確性始終很重要,並且始終參考Msdn文檔以獲取有關使用XmlDocument和XDocument的幫助。