且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

需要XPath帮助

更新时间:2023-02-26 14:23:49



xmlDoc.DocumentElement.SelectSingleNode(" // PresentationGroup [last()]")



xmlDoc .DocumentElement.SelectSingleNode(" / Form / Content / Line / PresentationGroup [last()]")

-

HTH,

Phillip Williams
http://www.societopia.net
http://www.webswapp.com

" Joe&quot ;写道:

xmlDoc.DocumentElement.SelectSingleNode("//PresentationGroup[last()]")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line/PresentationGroup[last()]")
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Joe" wrote:
大家好:

我有一个带有以下结构的xml文档,其中Form是
文档元素:

<表格>
<内容>
<行>等等等等等等<行>
< PresentationGroup>
其他标签
< / PresentationGroup>
< / Line>
< Line> blah blah blah 2< / Line>
< Line>
&lt ; PresentationGroup>
其他标签
< / PresentationGroup>
< / Line>
< / Content>
< / Form>
节点数从一个文档更改为下一个文档)。我试过了
Content / Line / PresentationGroup [last()]我什么也没得到。

我的代码如下:

xmlDoc.DocumentElement.SelectSingleNode(" Content / Line / PresentationGroup [last()]")

这将返回Nothing。我不知道为什么,虽然我很确定它很简单!

任何人都可以看到我做错了吗?
> 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



Phillip,


我试过这两个。由于某些原因,每个返回第一个

PresentationGroup。知道为什么吗?


-

Joe

" Phillip Williams"写道:
Phillip,

I have tried both of these. For some reason each returns the first
PresentationGroup. Any idea why?

--
Joe
"Phillip Williams" wrote:

xmlDoc.DocumentElement.SelectSingleNode(" // PresentationGroup [last()]")

xmlDoc.DocumentElement。 SelectSingleNode(" / Form / Content / Line / PresentationGroup [last()]")

-
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com

" Joe"写道:

xmlDoc.DocumentElement.SelectSingleNode("//PresentationGroup[last()]")
or
xmlDoc.DocumentElement.SelectSingleNode("/Form/Content/Line/PresentationGroup[last()]")
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
"Joe" wrote:
大家好:

我有一个带有以下结构的xml文档,其中Form是
文档元素:

<表格>
<内容>
<行>等等等等等等<行>
< PresentationGroup>
其他标签
< / PresentationGroup>
< / Line>
< Line> blah blah blah 2< / Line>
< Line>
&lt ; PresentationGroup>
其他标签
< / PresentationGroup>
< / Line>
< / Content>
< / Form>
节点数从一个文档更改为下一个文档)。我试过了
Content / Line / PresentationGroup [last()]我什么也没得到。

我的代码如下:

xmlDoc.DocumentElement.SelectSingleNode(" Content / Line / PresentationGroup [last()]")

这将返回Nothing。我不知道为什么,虽然我很确定它很简单!

任何人都可以看到我做错了吗?
> 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&gt ;
<内容>
< 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>&quot ;;

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/