gpt4 book ai didi

c# .net xpath 遍历时不挑选元素

转载 作者:行者123 更新时间:2023-12-02 01:25:06 25 4
gpt4 key购买 nike

我正在开发一个页面,该页面使用 XPATH 遍历 XML 文档以提取某些数据元素并基于它们构建字符串。我能够正确计算元素,但是当尝试排序遍历它们时,某些正在计算的元素没有显示。该任务很可能可以更有效地完成,任何有关正确完成此任务的帮助将不胜感激。

XML

<?xml version="1.0" encoding="UTF-16"?>
<Presentation>
<Filename>Name of file</Filename>
<version>1.2</version>
<threshold>23</threshold> <!-- gives number of slides -->
<Slides>
<Slide id="slide id">
<Filename>Name of file</Filename>
<Title>Title of slide</Title>
</Slide>
<Slide id="slide id">
<Filename>Name of file</Filename>
<Title>Title of slide</Title>
<quizobjects>
<quizobject id="1">
<filename>Name of quiz</filename>
</quizobjects>
</Slide>
<Slide id="slide id">
<Filename>Name of file</Filename>
<Title>Title of slide</Title>
</Slide>
...etc
</Slides>
</Presentation>

这是 XML 的示例。我遍历幻灯片并数出测验对象。 (这会返回正确的数字)但是,当我遍历所有幻灯片试图获取“幻灯片”节点中每个测验的位置时,它永远不会命中任何测验对象。

C#

int numSlides;

XmlDocument doc = new XmlDocument();
doc.LoadXml(xmlToParse.Text); //pass in xml string
XmlElement root = doc.DocumentElement;

//get number of slides from threshold node
numSlides = Convert.ToInt32(root.SelectSingleNode("//threshold").InnerText);

//get number of quizzes/slides
XmlNodeList xnQuiz = root.SelectNodes("/Presentation/Slides/Slide/quizobjects"); //returns 7
XmlNodeList xnList = root.SelectNodes("/Presentation/Slides/Slide");

int[] quizLocArray = new int[xnQuiz.Count]; //create array to hold location of quizzes

int j = 0;
//find index of quizzes in slide list
for(int i = 0; i < xnList.Count; i++)
{
XmlNode quiz = xnList[i].SelectSingleNode("/quizobjects");
if(quiz != null) //stepping through quiz always equals null
{
quizLocArray[j] = (i + 1);
j++;
}
}

输出

numSlides/XML 中的幻灯片总数:23

xnQuiz.Count/XML 中的测验总数:7

String.Join(",", quizLocArray)/幻灯片列表中测验索引数组:0,0,0,0,0,0,0

最佳答案

您的 XML 数据似乎格式不正确,因为您缺少结尾 </quizobject> 。我将继续假设这只是一个错字。

您需要将 XPath 查询从 SelectSingleNode("/quizobjects") 更改为至SelectSingleNode("quizobjects")获取所需的 XML 元素。

Presentation.xml 文件:

<Presentation>
<Filename>Name of file</Filename>
<version>1.2</version>
<threshold>23</threshold>

<!-- gives number of slides -->
<Slides>
<Slide id="slide id">
<Filename>Name of file</Filename>
<Title>Title of slide</Title>
</Slide>
<Slide id="slide id">
<Filename>Name of file</Filename>
<Title>Title of slide</Title>
<quizobjects>
<quizobject id="1">
<filename>Name of quiz</filename>
</quizobject>
</quizobjects>
</Slide>
<Slide id="slide id">
<Filename>Name of file</Filename>
<Title>Title of slide</Title>
</Slide>
</Slides>

</Presentation>

C# 代码:

XmlDocument doc     = new XmlDocument();

try
{
using (var reader = XmlReader.Create("Presentation.xml"))
{
int numSlides;

doc.Load(reader);

XmlElement root = doc.DocumentElement;

//get number of slides from threshold node
numSlides = Convert.ToInt32(root.SelectSingleNode("//threshold").InnerText);

//get number of quizzes/slides
XmlNodeList xnQuiz = root.SelectNodes("/Presentation/Slides/Slide/quizobjects");
XmlNodeList xnList = root.SelectNodes("/Presentation/Slides/Slide");

//create array to hold location of quizzes
int[] quizLocArray = new int[xnQuiz.Count];

int j = 0;

//find index of quizzes in slide list
for (int i = 0; i < xnList.Count; i++)
{
XmlNode quiz = xnList[i].SelectSingleNode("quizobjects");
if (quiz != null)
{
quizLocArray[j] = (i + 1);
j++;
}
}
}
}
catch (Exception exception)
{
Console.WriteLine(exception.Message);
}

关于c# .net xpath 遍历时不挑选元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6274461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com