How to get element content using only XPath and C# .NET

前端 未结 3 1417
粉色の甜心
粉色の甜心 2021-01-13 23:56

I\'ve found a lot of articles about how to get node content by using simple XPath expression and C#, for example:

XPath:

/bookstore/author/first-nam         


        
相关标签:
3条回答
  • After googling c# .net xpath for few seconds you'll find this article, which provides example which you can easily modify to use XPathDocument, XPathNavigator and XPathNavigator::SelectSingleNode():

    XPathNavigator nav;
    XPathDocument docNav;
    string xPath;
    
    docNav = new XPathDocument("c:\\books.xml");
    nav = docNav.CreateNavigator();
    xPath = "/Cell/CellContent/Para/ParaLine/String/text()";
    
    string value = nav.SelectSingleNode(xPath).Value
    

    I recommend more reading on xPath syntax. Much more.

    0 讨论(0)
  • 2021-01-14 00:26

    You can use Linq to XML as well to get value of specified element

    var list = XDocument.Parse("xml string").Descendants("ParaLine")
                     .Select(x => x.Element("string").Value).ToList();
    

    From above query you will get value of all the string element which are inside ParaLine tag.

    0 讨论(0)
  • 2021-01-14 00:27
    navigator.SelectSingleNode("/Cell/CellContent/Para/ParaLine/String/text()").Value
    
    0 讨论(0)
提交回复
热议问题