TIA, TIA, - Joe Hello All: I have an xml document with the following structure, where Form is the document element: <Form> <Content> <Line>blah blah blah</Line> <Line> <PresentationGroup> miscellaneous tags </PresentationGroup> </Line> <Line>blah blah blah 2</Line> <Line> <PresentationGroup> miscellaneous tags </PresentationGroup> </Line> </Content> </Form> I want an XPath to the last PresentationGroup node (there are multiple PresentationGroup nodes per xml document and the number of PresentationGroup nodes per document changes from one document to the next). I''ve tried "Content/Line/PresentationGroup[last()]" and am getting nothing. My code looks like: xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]") This returns Nothing. I can''t figure out why, although I''m sure that it''s simple! Can anyone see what I am doing wrong? TIA, -- Joe Joe写道:Joe wrote:我有一个具有以下结构的xml文档,其中Form是文档元素: < Form> ; <内容> < Line> blah blah blah< / Line> < Line> < PresentationGroup> 杂项标签 < / PresentationGroup> < / Line> < Line> blah blah blah 2< / Line> < Line> < PresentationGroup> 其他标签< / PresentationGroup> < / Line> < / Content> < / Form> xmlDoc.DocumentElement.SelectSingleNode(" Content / Line / PresentationGroup [last()]") I have an xml document with the following structure, where Form is the document element: <Form> <Content> <Line>blah blah blah</Line> <Line> <PresentationGroup> miscellaneous tags </PresentationGroup> </Line> <Line>blah blah blah 2</Line> <Line> <PresentationGroup> miscellaneous tags </PresentationGroup> </Line> </Content> </Form> xmlDoc.DocumentElement.SelectSingleNode("Content/Line/PresentationGroup[last()]") 这是一个包含你的完整C#代码片段提供XML作为输入 XML和你提供的XPath表达式,当然找到了元素: string xmlMarkup = @"< Form> < Content> < Line> blah blah blah< / Line> < Line> < PresentationGroup> 其他标签 < / PresentationGroup> < / Line> < Line> blah blah blah 2< / Line> < Line> < PresentationGroup> 其他标签 < / PresentationGroup> < / Line> < / Content> < / Form>" ;; XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(xmlMarkup); XmlElement presentationGroup = xmlDocu ment.DocumentElement.SelectSingleNode(@" Con tent / Line / PresentationGroup [last()]") as XmlElement; if(presentationGroup!= null){ Console.WriteLine(presentationGroup.OuterXml); } else { Console.WriteLine("找不到任何元素。); } 因此,您的真实代码和/或真实XML更复杂。只是 猜测,该XML文档是否声明了名称空间,特别是 默认名称空间,例如 < Form xmlns =" http:/ /example.com/2006/ns1"> ?然后检查 < http://www.faqts.com/knowledge_base/view.phtml/aid/34022/fid/616> - Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ 这篇关于需要XPath帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 23:36