Linq to xml 输出某个元素的值

发布于 2024-11-01 15:08:19 字数 5745 浏览 2 评论 0原文

抱歉,下面的 XML 示例很长。它有两条记录,每条记录有三个相似的节点 ()。我想要完成的是获取第二个节点 的值。在下面的代码中,我获取了所有三个节点的值,但我想要类似 get elementat(1) 的内容。感谢您的帮助。 我尝试过这个但没有成功。

MsgBox(xEle.Elements.ElementAt(1).Value)
Dim xd As XDocument = XDocument.Load("C:\Users\stopete\Desktop\Marc_conv ertxml.xml")
If xd IsNot Nothing Then
Dim datafields = From datafield In xd.Descendants("datafield")
Where (datafield.Attribute("tag").Value = "500")
Select datafield

For Each xEle In datafields

MsgBox(xEle)

Next

End If

XML:

<?xml version="1.0"?>
<collection>
<record>
<leader>03799cam 2200493Ia 4500</leader>
<controlfield tag="001">648168072
<subfield code="a">Battle, Stephen L.</subfield>
</controlfield>
<controlfield tag="003">SIRSI</controlfield>
<controlfield tag="005">20110207150903.0</controlfield>
<controlfield tag="008">100716s2010 caub bm f000 0 eng d</controlfield>
<controlfield tag="007">cr b|||||a||||</controlfield>
<datafield tag="035" ind1=" " ind2=" ">
<subfield code="a">AD#</subfield>
<subfield code="c">AD#</subfield>
<subfield code="d">AD#</subfield>
</datafield>
<datafield tag="037" ind1=" " ind2=" ">
<subfield code="a">hey</subfield>
</datafield>
<datafield tag="040" ind1=" " ind2=" ">
<subfield code="a">ADA524509</subfield>
<subfield code="b">DTI</subfield>
</datafield>
<datafield tag="043" ind1=" " ind2=" ">
<subfield code="a">n-us---</subfield>
</datafield>
<datafield tag="049" ind1=" " ind2=" ">
<subfield code="a">AD#A</subfield>
</datafield>
<datafield tag="100" ind1="1" ind2=" ">
<subfield code="a">Battle, Stephen L.</subfield>
</datafield>
<datafield tag="245" ind1="1" ind2="0">
<subfield code="a">Lessons in legitimacy</subfield>
<subfield code="h">[electronic resource] :</subfield>
<subfield code="b">the LTTE end-game of 2007--2009 /</subfield>
<subfield code="c">Stephen L. Battle.</subfield>
</datafield>
<datafield tag="260" ind1=" " ind2=" ">
<subfield code="a">Monterey, California :</subfield>
<subfield code="b">Naval Postgraduate School,</subfield>
<subfield code="c">2010.</subfield>
</datafield>
<datafield tag="300" ind1=" " ind2=" ">
<subfield code="a">xiv, 57 p. :</subfield>
<subfield code="b">ill. (1 col map) ;</subfield>
<subfield code="c">28 cm.</subfield>
</datafield>
<datafield tag="500">
<subfield code="a">Thesis Advisor(s): Borer, Douglas A. ; Second Reader: Chatterjee, Anshu.</subfield>
</datafield>
<datafield tag="500">
<subfield code="a">"June 2010."</subfield>
</datafield>
<datafield tag="500">
<subfield code="a">Author(s) subject terms: LTTE, Counter Insurgency, COIN, Sri Lanka, Tamil Eelam, Eelam War IV, SLAF, South Asia.</subfield>
</datafield>
</record>
<record>
<leader>02919cam 2200493Ia 4500</leader>
<controlfield tag="001">648167554</controlfield>
<controlfield tag="003">SIRSI</controlfield>
<controlfield tag="005">20110207150310.0</controlfield>
<controlfield tag="008">100716s2010 caua bm f000 0 eng d</controlfield>
<controlfield tag="007">cr b|||||a||||</controlfield>
<datafield tag="035" ind1=" " ind2=" ">
<subfield code="a">AD#</subfield>
<subfield code="c">AD#</subfield>
<subfield code="d">AD#</subfield>
</datafield>
<datafield tag="037" ind1=" " ind2=" ">
<subfield code="a">hey</subfield>
</datafield>
<datafield tag="040" ind1=" " ind2=" ">
<subfield code="a">ADA524541</subfield>
<subfield code="b">DTI</subfield>
</datafield>
<datafield tag="043" ind1=" " ind2=" ">
<subfield code="a">n-us---</subfield>
</datafield>
<datafield tag="049" ind1=" " ind2=" ">
<subfield code="a">AD#A</subfield>
</datafield>
<datafield tag="100" ind1="1" ind2=" ">
<subfield code="a">Battaglia, Neal F.</subfield>
</datafield>
<datafield tag="245" ind1="1" ind2="0">
<subfield code="a">Utility of satellite LIDAR waveform data in shallow water</subfield>
<subfield code="h">[electronic resource] /</subfield>
<subfield code="c">Neal F. Battaglia.</subfield>
</datafield>
<datafield tag="260" ind1=" " ind2=" ">
<subfield code="a">Monterey, California :</subfield>
<subfield code="b">Naval Postgraduate School,</subfield>
<subfield code="c">2010.</subfield>
</datafield>
<datafield tag="300" ind1=" " ind2=" ">
<subfield code="a">xvi, 67 p. :</subfield>
<subfield code="b">ill. (some col.) ;</subfield>
<subfield code="c">28 cm.</subfield>
</datafield>
<datafield tag="500">
<subfield code="a">Thesis Advisor(s): Olsen, Richard C. ; Second Reader: Trask, David M.</subfield>
</datafield>
<datafield tag="500">
<subfield code="a">"June 2010."</subfield>
</datafield>
<datafield tag="500”>
<subfield code="a">Author(s) subject terms: LIDAR Waveforms, Underwater LIDAR signature, Spaceborne LIDAR.</subfield>
</datafield>
</record>
</collection> 

Sorry for the long XML sample below. It has two records and each record has three similar Nodes (<datafield tag="500">). What I want to accomplish is get the value of the second Node <datafield tag = “5000”>. The following code I gets the value for all three nodes but I want something like get elementat(1). Appreciate your help.
I tried this but it didn’t worked.

MsgBox(xEle.Elements.ElementAt(1).Value)
Dim xd As XDocument = XDocument.Load("C:\Users\stopete\Desktop\Marc_conv ertxml.xml")
If xd IsNot Nothing Then
Dim datafields = From datafield In xd.Descendants("datafield")
Where (datafield.Attribute("tag").Value = "500")
Select datafield

For Each xEle In datafields

MsgBox(xEle)

Next

End If

XML:

<?xml version="1.0"?>
<collection>
<record>
<leader>03799cam 2200493Ia 4500</leader>
<controlfield tag="001">648168072
<subfield code="a">Battle, Stephen L.</subfield>
</controlfield>
<controlfield tag="003">SIRSI</controlfield>
<controlfield tag="005">20110207150903.0</controlfield>
<controlfield tag="008">100716s2010 caub bm f000 0 eng d</controlfield>
<controlfield tag="007">cr b|||||a||||</controlfield>
<datafield tag="035" ind1=" " ind2=" ">
<subfield code="a">AD#</subfield>
<subfield code="c">AD#</subfield>
<subfield code="d">AD#</subfield>
</datafield>
<datafield tag="037" ind1=" " ind2=" ">
<subfield code="a">hey</subfield>
</datafield>
<datafield tag="040" ind1=" " ind2=" ">
<subfield code="a">ADA524509</subfield>
<subfield code="b">DTI</subfield>
</datafield>
<datafield tag="043" ind1=" " ind2=" ">
<subfield code="a">n-us---</subfield>
</datafield>
<datafield tag="049" ind1=" " ind2=" ">
<subfield code="a">AD#A</subfield>
</datafield>
<datafield tag="100" ind1="1" ind2=" ">
<subfield code="a">Battle, Stephen L.</subfield>
</datafield>
<datafield tag="245" ind1="1" ind2="0">
<subfield code="a">Lessons in legitimacy</subfield>
<subfield code="h">[electronic resource] :</subfield>
<subfield code="b">the LTTE end-game of 2007--2009 /</subfield>
<subfield code="c">Stephen L. Battle.</subfield>
</datafield>
<datafield tag="260" ind1=" " ind2=" ">
<subfield code="a">Monterey, California :</subfield>
<subfield code="b">Naval Postgraduate School,</subfield>
<subfield code="c">2010.</subfield>
</datafield>
<datafield tag="300" ind1=" " ind2=" ">
<subfield code="a">xiv, 57 p. :</subfield>
<subfield code="b">ill. (1 col map) ;</subfield>
<subfield code="c">28 cm.</subfield>
</datafield>
<datafield tag="500">
<subfield code="a">Thesis Advisor(s): Borer, Douglas A. ; Second Reader: Chatterjee, Anshu.</subfield>
</datafield>
<datafield tag="500">
<subfield code="a">"June 2010."</subfield>
</datafield>
<datafield tag="500">
<subfield code="a">Author(s) subject terms: LTTE, Counter Insurgency, COIN, Sri Lanka, Tamil Eelam, Eelam War IV, SLAF, South Asia.</subfield>
</datafield>
</record>
<record>
<leader>02919cam 2200493Ia 4500</leader>
<controlfield tag="001">648167554</controlfield>
<controlfield tag="003">SIRSI</controlfield>
<controlfield tag="005">20110207150310.0</controlfield>
<controlfield tag="008">100716s2010 caua bm f000 0 eng d</controlfield>
<controlfield tag="007">cr b|||||a||||</controlfield>
<datafield tag="035" ind1=" " ind2=" ">
<subfield code="a">AD#</subfield>
<subfield code="c">AD#</subfield>
<subfield code="d">AD#</subfield>
</datafield>
<datafield tag="037" ind1=" " ind2=" ">
<subfield code="a">hey</subfield>
</datafield>
<datafield tag="040" ind1=" " ind2=" ">
<subfield code="a">ADA524541</subfield>
<subfield code="b">DTI</subfield>
</datafield>
<datafield tag="043" ind1=" " ind2=" ">
<subfield code="a">n-us---</subfield>
</datafield>
<datafield tag="049" ind1=" " ind2=" ">
<subfield code="a">AD#A</subfield>
</datafield>
<datafield tag="100" ind1="1" ind2=" ">
<subfield code="a">Battaglia, Neal F.</subfield>
</datafield>
<datafield tag="245" ind1="1" ind2="0">
<subfield code="a">Utility of satellite LIDAR waveform data in shallow water</subfield>
<subfield code="h">[electronic resource] /</subfield>
<subfield code="c">Neal F. Battaglia.</subfield>
</datafield>
<datafield tag="260" ind1=" " ind2=" ">
<subfield code="a">Monterey, California :</subfield>
<subfield code="b">Naval Postgraduate School,</subfield>
<subfield code="c">2010.</subfield>
</datafield>
<datafield tag="300" ind1=" " ind2=" ">
<subfield code="a">xvi, 67 p. :</subfield>
<subfield code="b">ill. (some col.) ;</subfield>
<subfield code="c">28 cm.</subfield>
</datafield>
<datafield tag="500">
<subfield code="a">Thesis Advisor(s): Olsen, Richard C. ; Second Reader: Trask, David M.</subfield>
</datafield>
<datafield tag="500">
<subfield code="a">"June 2010."</subfield>
</datafield>
<datafield tag="500”>
<subfield code="a">Author(s) subject terms: LIDAR Waveforms, Underwater LIDAR signature, Spaceborne LIDAR.</subfield>
</datafield>
</record>
</collection> 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

2024-11-08 15:08:19

不是 VB 人员,但这应该可行:

Dim xd As XDocument = XDocument.Load("C:\Users\stopete\Desktop\Marc_conv ertxml.xml")
If xd IsNot Nothing Then
Dim datafields = From datafield In xd.Descendants("datafield")
Where (datafield.Attribute("tag").Value = "500")
Select datafield
Skip 1
Take 1

或者如果您不想更改查询:

MsgBox(xEle.Elements.Skip(1).Take(1).Single().Value)

Not a VB guy but this should work:

Dim xd As XDocument = XDocument.Load("C:\Users\stopete\Desktop\Marc_conv ertxml.xml")
If xd IsNot Nothing Then
Dim datafields = From datafield In xd.Descendants("datafield")
Where (datafield.Attribute("tag").Value = "500")
Select datafield
Skip 1
Take 1

Or if you don't want to change the query:

MsgBox(xEle.Elements.Skip(1).Take(1).Single().Value)
咆哮 2024-11-08 15:08:19
 Dim xd As XDocument = XDocument.Load(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "XMLFile1.xml"), LoadOptions.None)
        If xd IsNot Nothing Then


            Dim ele As XElement = xd.Descendants("datafield").Where(Function(a) a.Attribute("tag").Value = "500")(1)

            MsgBox(ele.Value)


        End If
 Dim xd As XDocument = XDocument.Load(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "XMLFile1.xml"), LoadOptions.None)
        If xd IsNot Nothing Then


            Dim ele As XElement = xd.Descendants("datafield").Where(Function(a) a.Attribute("tag").Value = "500")(1)

            MsgBox(ele.Value)


        End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文