【问题标题】:Retrieving next xml node by previous node's value - using linq通过前一个节点的值检索下一个 xml 节点 - 使用 linq
【发布时间】:2016-08-18 10:38:49
【问题描述】:

我有以下 xml:

<century>  
 <question>What is silvia</question>  
 <answer>silvia is artificial intelligence</answer>  
 <question>What is your name</question> 
  <answer>my name is RAMESH</answer> 
</century>  

我想根据使用 linq 的给定问题获得答案。我该怎么做?

当前代码:

string textToSearch = "When did AI research start"; 
XDocument doc = XDocument.Load(@"D:\SILVIA\New task\FinalXml.xml"); 

var q = doc.Descendants("CenturySoft")
           .Descendants("question")
           .Where(item => item.Value == textToSearch)
           .Select(item => item.NextNode)
           .FirstOrDefault();

我得到带有&lt;answer&gt; 标签的输出。怎么去掉?

【问题讨论】:

    标签: c# linq linq-to-xml


    【解决方案1】:

    这只有在 q 和 a 是有序对的情况下才有效

        Dim xe As XElement
        'to load from a file
        '  xe = XElement.Load("Your Path Here")
    
        ' for testing
        xe = <century>
                 <question>What is silvia</question>
                 <answer>silvia is artificial intelligence</answer>
                 <question>What is your name</question>
                 <answer>my name is RAMESH</answer>
             </century>
    
        Dim qs As IEnumerable(Of XElement) = xe...<question>
        Dim ans As IEnumerable(Of XElement) = xe...<answer>
    
    
        For x As Integer = 0 To qs.Count - 1
            Debug.WriteLine("Q: {0}?  A: {1}", qs(x).Value, ans(x).Value)
        Next
    

    如果 XML 有不同的结构(更好?)这会更容易。

        Dim xe As XElement
        xe = <QandA>
                 <item>
                     <question>What is silvia</question>
                     <answer>silvia is artificial intelligence</answer>
                 </item>
                 <item>
                     <question>What is your name</question>
                     <answer>my name is RAMESH</answer>
                 </item>
             </QandA>
    

    【讨论】:

      【解决方案2】:

      这样做的一种方法是使用 Linq to Xml:

      var result = XDocument.Load("data.xml")
               .Descendants("century")
               .SelectMany(element => element.Descendants("question")
                                             .Zip(element.Descendants("answer"),
                                                  (q,a) => new { 
                                                      Question = q.Value, 
                                                      Answer = a.Value 
                                                  }))
               .ToList();
      

      请注意,如果您对一个问题有多个答案,或者如果您对问题的顺序有疑问,则此方法效果不佳,请回答

      另一种方法(使用XPathSelectElement扩展方法-添加using System.Xml.XPath)是:

      var question = "What is silvia";
      
      var answer = XDocument.Load("data.xml")
                            .XPathSelectElement($"century/question[\"{question}\"]")
                            .NextNode;
      

      更新后,您可以:

      var q = doc.Descendants("century")
                 .Descendants("question")
                 .Where(item => item.Value == textToSearch)
                 .Select(item => item.ElementsAfterSelf().FirstOrDefault())
                 .FirstOrDefault().Value;
      

      当使用NextNode 时,你会得到一个XNode 实例,这是一个更难使用的类,所以你可以像上面那样做。

      【讨论】:

      • 没有没有解决。我有这样的 linq 查询字符串 textToSearch = "AI 研究何时开始"; XDocument doc = XDocument.Load(@"D:\SILVIA\New task\FinalXml.xml"); var q = doc.Descendants("CenturySoft").Descendants("question") .Where(item => item.Value == textToSearch) .Select(item => item.NextNode) .FirstOrDefault();在这一点上,我得到的输出是 我认为是的,但我们还没有达到可以开始这个过程的 AI 水平。。我希望输出没有答案标签。请提供任何解决方案。谢谢。
      • @BhimanaRamesh - 如果您可以删除您对代码的评论,如果您可以投票并接受我的回答,我将非常感激
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多