简体   繁体   English

在XDocument中搜索特定的节点值

[英]Search specific node value in XDocument

As my XML getting larger and larger. 随着我的XML越来越大。 I am trying to find ways to search my XML even faster. 我正在尝试找到更快地搜索我的XML的方法。 I have read about this XDocument compared that this is much faster than XMLDocument but I am unable to know if this is true as I am not familiar with XML.Linq syntax. 我已经阅读过有关XDocument ,它比XMLDocument快得多,但是由于我不熟悉XML.Linq语法,所以我不知道这是否成立。

This is my sample XML. 这是我的示例XML。

<?xml version="1.0" encoding="utf-8"?>
<Application>
    <Library>
        <Track>
            <TrackID>1</TrackID>
            <Name>Sample</Name>
            <Artist>ArtistName</Artist>
            <Location>C:\Users\User\Music\01 File.m4a</Location>
        </Track>
        ...
    <Library>
</Application>

This is how I search a specific TrackID in my XML. 这就是我在XML中搜索特定TrackID

Dim nodeList As XmlNodeList = Document.SelectNodes("/iTunesCrimson/Library/Track"), trackID As Integer = 0
            For Each n As XmlNode In nodeList
                If Not track.Location = "" AndAlso n.SelectSingleNode("Location").InnerText = track.Location Then
                    trackID = Integer.Parse(n.SelectSingleNode("TrackID").InnerText)
                    Exit For
                End If
            Next
        Return trackID

And this is how I change the value of a specific node. 这就是我更改特定节点的值的方式。

Document.SelectSingleNode("/Application/Library/Track[TrackID=" & TrackID & "]/Name").InnerText = "Sample Value"

I am having a hard time figuring out how Linq syntax works as I had a hard time learning XMLDocument . 在学习XMLDocument ,我很难弄清Linq语法的工作方式。 Can someone help me convert these functions into Linq for XDocument as I am not familiar with it at the moment. 有人可以帮我将这些函数转换为XDocument Linq,因为我目前对此还不熟悉。

My sample code is in VB.Net but C# will do. 我的示例代码在VB.Net中,但C#可以。

I saw this here and I tried but gives me a NullReferenceException . 我在这里看到了这个,我尝试了但给了我NullReferenceException

string id = "123"; // id to be selected

XElement Contact = (from xml2 in XMLDoc.Descendants("Node")
                    where xml2.Element("ID").Value == id
                    select xml2).FirstOrDefault();

Console.WriteLine(Contact.ToString());

If you want to search the Track node which contains some specific TrackId, you can use this:- 如果要搜索包含某些特定TrackId的Track节点,可以使用以下方法:

var result = xdoc.Descendants("Track")
                             .FirstOrDefault(x => (string)x.Element("TrackID") == id);

Here, xdoc is XDocument object: XDocument xdoc = XDocument.Load(YourXMLFile); 在这里, xdoc是XDocument对象: XDocument xdoc = XDocument.Load(YourXMLFile);

If you are looking to find and update the TrackID you can do it like this:- 如果您要查找和更新TrackID ,可以执行以下操作:-

xdoc.Descendants("Track").FirstOrDefault(x => (string)x.Element("TrackID") == id)
                         .SetElementValue("TrackID", "2");

Finally save your XML:- 最后保存您的XML:

xdoc.Save(YourXMLfile);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM