我試圖隔離下面 XML 中<string>每個元素中的元素。<question>我使用的是我最熟悉的Xpath?;旧?,我使用 a SelectNodes("question"),如果指向下面的 XML,它將返回正確的 5 個元素。然后我想迭代<string>每個 內(nèi)的元素<question>。我不想直接轉到“問題/字符串”,因為它將返回<string>XML 文件中的所有實例。我有一些工作要做,其中未包含在此處,實際文件比這復雜得多,但這就是我正在努力解決的問題。這是我使用的代碼。它返回<string>文件的所有元素,而不僅僅是我在任何給定時刻正在使用的節(jié)點。XmlNodeList questions = doc.SelectNodes("//question");string question = null;foreach (XmlElement qquestion in questions) //I also tried XmlNode here{ XmlNodeList qstrings = qquestion.SelectNodes("//string"); foreach (XmlNode qstring in qstrings) { question = qstring.InnerText; //There's a lot more processing I'll do here } }這是我的簡化 XML。感謝您的指點。<content version="1.0"> <question> <string>Question 1 part 1</string> <string>Question 1 part 2</string> <graphic scale=".8" align="center" yOffset="-50" xOffset="50" asset="Numline_Triangle"/> <graphic scale="1.2" align="center" yOffset="20" asset="Numline_Shapes_0_40"/> </question> <question> <string>Question 2</string> <graphic scale=".8" align="center" yOffset="-50" xOffset="50" asset="Numline_Square"/> <graphic scale="1.2" align="center" yOffset="20" asset="Numline_Shapes_0_40"/> </question> <question> <string>Question 3 part 1</string> <string>Question 3 part 2</string> <graphic scale=".8" align="center" yOffset="-50" xOffset="50" asset="Numline_Square"/> <graphic scale="1.2" align="center" yOffset="20" asset="Numline_Shapes_60_100"/> </question> <question> <string>Question 4</string> <graphic scale=".8" align="center" yOffset="-50" xOffset="50" asset="Numline_Circle"/> <graphic scale="1.2" align="center" yOffset="20" asset="Numline_Shapes_60_100"/> </question> <question> <string>Question 5</string> <graphic scale=".8" align="center" yOffset="-50" xOffset="50" asset="Numline_Triangle"/> <graphic scale="1.2" align="center" yOffset="20" asset="Numline_Shapes_60_100"/> </question> </content>
C# 中的嵌套 Xml 節(jié)點列表
慕田峪7331174
2023-08-27 10:47:09