2013-05-02 75 views
0

我想獲得一個xpath表達式的值使用SelectSingleNode小寫,是否有一個Xpath,可以給我一個評估xpath表達式的小寫值?SelectSingleNode來評估LowerCase節點集值

有可能通過有一個xsl轉換表,但它可能與下面?

該函數期望具有nodeset類型,下面評估爲字符串,從而引發異常。是否有可能返回的字符串轉換爲節點集做出的SelectSingleNode快樂

 string xml = "<bac><test>Val</test></bac>"; 
     string xpath = "/bac/test/text()"; 

     var transpath = "translate(/bac/test/text(),'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"; 

     StringReader sReader = new StringReader(xml); 

     XmlReader xReader = new XmlTextReader(sReader);    
     XPathDocument xmlPathDoc = new XPathDocument(xReader); 
     XPathNavigator nav = xmlPathDoc.CreateNavigator(); 

     XPathNodeIterator nodeIterator = nav.Select("/"); 

     XPathNavigator navNode = nodeIterator.Current; 
     var v = navNode.SelectSingleNode(transpath).Value; 
     // expect val 

回答

1

如果你有一個返回原始值(字符串,數字,布爾值)的XPath表達式,而不是一組節點,你需要使用XPathNavigator上的Evaluate方法,見http://msdn.microsoft.com/en-us/library/2c16b7x8.aspx

因此,對於你的代碼可以把它簡化爲

string xml = "<bac><test>Val</test></bac>"; 
    string xpath = "/bac/test"; 

    var transpath = "translate(/bac/test,'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz')"; 

    XPathNavigator nav; 
    using (StringReader sr = new StringReader(xml)) 
    { 
     nav = new XPathDocument(sr).CreateNavigator(); 
    } 
    string v = nav.Evaluate(transpath) as string; 

在另一方面.NET是字符串操作要強於的XPath 1.0,所以我會非常想使用

XPathNavigator test = nav.SelectSingleNode(path); 
string v = test.Value.ToLower();