C# 按上一个节点检索下一个xml节点';s值-使用linq
我有以下xml:C# 按上一个节点检索下一个xml节点';s值-使用linq,c#,linq,linq-to-xml,C#,Linq,Linq To Xml,我有以下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>
<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 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
扩展方法-add使用System.Xml.XPath
)是:
更新后,您可以执行以下操作:
var q = doc.Descendants("century")
.Descendants("question")
.Where(item => item.Value == textToSearch)
.Select(item => item.ElementsAfterSelf().FirstOrDefault())
.FirstOrDefault().Value;
当使用
NextNode
时,您会得到一个更难使用的类的实例,因此您可以像上面那样操作。这只会在存在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
Dim xe As XElement
'从文件加载
'xe=XElement.Load(“此处的路径”)
"测试",
xe=
西尔维亚是什么
西尔维亚是人工智能
你叫什么名字?
我叫拉梅什
尺寸qs为IEnumerable(像素)=xe。。。
Dim ans作为IEnumerable(元素的)=xe。。。
对于x作为整数=0到qs.Count-1
WriteLine(“Q:{0}?A:{1}”,qs(x).Value,ans(x).Value)
下一个
如果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>
Dim xe As XElement
xe=
西尔维亚是什么
西尔维亚是人工智能
你叫什么名字?
我叫拉梅什
否未解决。我有这样的linq查询字符串textToSearch=“AI研究是什么时候开始的”;XDocument doc=XDocument.Load(@“D:\SILVIA\New task\FinalXml.xml”);var q=doc.substands(“CenturySoft”).substands(“问题”).Where(item=>item.Value==textToSearch)。选择(item=>item.NextNode)。FirstOrDefault();在这方面,我得到了我认为是的输出,但我们还没有达到这个过程可以开始的AI水平。。我希望输出为无应答标记。请提供任何解决方案。谢谢。@BhimanaRamesh-如果您可以用代码删除您的评论,如果您可以投票并接受我的回答,我将非常感激
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
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>