Get the value between <> with dynamic number inside it

前端 未结 2 683
慢半拍i
慢半拍i 2021-01-15 19:25

I am working on a text summarization method ,for test my method i have a benchmark called doc 2007 ,inside this benchmark i have a lot of xml file ,i should cle

相关标签:
2条回答
  • 2021-01-15 19:59

    XElment.Parse only use in String with single root node. The instance you wrote have two nodes '' without one root node. You can add a root node like below:

    xml = "<root>" + xml + "</root>";
    
    0 讨论(0)
  • 2021-01-15 20:10

    Using LINQ to XML should be easier:

    var res = XElement.Parse(xml)
                      .Descendants("sentence").Where(e => e.Attribute("id").Value == "s0")
                      .FirstOrDefault().Value;
    

    or, as Yeldar suggested, the cleaner way would be:

    var s0 = XElement.Parse(xml)
                     .Descendants("sentence").FirstOrDefault(e => e.Attribute("id").Value == "s0")
                     .Value;
    
    0 讨论(0)
提交回复
热议问题