簡體   English   中英

將現有的xml元素添加到xml文檔中

[英]Add an existing xml element to a xml document

在此LINQ查詢中,我將為自己的帶有軌道對象的CD創建一個新的XDocument:

  var cdXml = new XDocument(new XElement("CD", new XAttribute("Artiest", c.Titel), new XAttribute("Naam", c.Naam),
            from t in c.tracks
            select new XElement("Tracks",
            new XElement("Track"),
            new XElement("Artiest", t.Artiest),
            new XElement("Titel", t.Titel),
            new XElement("Lengte", t.Lengte)
            )));

我正在使用音樂網站上的API,這就是我從中創建第二個XDocument的方式:

    String xmlString;
    using (WebClient wc = new WebClient())
    {
        xmlString = wc.DownloadString(@"http://link-to-method");
    }
    XDocument myXMLDoc = XDocument.Parse(xmlString);

之后,我想將myXMLDoc中的軌道添加到我自己的cdXml文檔中,我已經向cdXml文檔中添加了3條軌道,而我只想添加myXMLDoc中不在我的cdXml文檔中的軌道。

這是我的查詢,但不起作用:

            var query1 = from track in cdXml.Root.Elements("Tracks")
                        from track2 in myXMLDoc.Root.Element("album").Element("tracks").Elements("track")
                        where !track2.Element("name").Value.Contains(track.Element("Titel").Value)
                        select cdXml.Element("Tracks").Add(new XElement("Artiest", track2.Element("name").Value), 
new XElement("Titel", track2.Element("artist").Element("name").Value), 
new XElement("Lengte", track2.Element("duration").Value));

如何將myXMLDoc中的現有元素添加到cdXml?

這是來自2條軌道的API調用中的xml文件:

<lfm status="ok">
    <album>
        <name>Awake</name>
        <artist>Dream Theater</artist>
        <mbid>e5544c68-43e9-4754-9239-b618454557f4</mbid>
        <url>https://www.last.fm/music/Dream+Theater/Awake</url>
        <image size="small">https://lastfm-img2.akamaized.net/i/u/34s/96e5ac5821bf4a138aec1b8f80f25a6f.png</image>
        <listeners>216679</listeners>
        <playcount>6046178</playcount>
        <tracks>
            <track rank="1">
                <name>6:00</name>
                <url>https://www.last.fm/music/Dream+Theater/_/6:00</url>
                <duration>331</duration>
                <streamable fulltrack="0">0</streamable>
                <artist>
                    <name>Dream Theater</name>
                    <mbid>28503ab7-8bf2-4666-a7bd-2644bfc7cb1d</mbid>
                    <url>https://www.last.fm/music/Dream+Theater</url>
                </artist>
            </track>
            <track rank="2">
                <name>Caught in a Web</name>
                <url>https://www.last.fm/music/Dream+Theater/_/Caught+in+a+Web</url>
                <duration>328</duration>
                <streamable fulltrack="0">0</streamable>
                <artist>
                    <name>Dream Theater</name>
                    <mbid>28503ab7-8bf2-4666-a7bd-2644bfc7cb1d</mbid>
                    <url>https://www.last.fm/music/Dream+Theater</url>
                </artist>
            </track>
    </album>
</lfm>

我終於找到了自己的解決方案:

var query = from track in myXMLDoc.Root.Element("album").Element("tracks").Elements("track")
                        join track2 in cdXml.Root.Elements("Tracks") on track.Element("name").Value equals track2.Element("Titel").Value into joinedT
                        from track2 in joinedT.DefaultIfEmpty()
                        where track2 == null
                        select track;

首先,編寫查詢以返回要添加到第二個文檔中的元素。 只是。 編寫返回這些查詢的查詢。 在調試器中,確認您正在獲取想要的東西。

然后,編寫一個遍歷這些元素的foreach循環,並將每個元素添加到想要添加它們的任何對象。

您無法嘗試使用select添加東西。 你不能那樣做。 Linq不是您如何向集合中添加東西。 它僅用於編寫查詢。

我認為這可能是您要嘗試執行的操作,但我不了解第一個查詢中的track / track2

var query1 = 
    from track in cdXml.Root.Elements("Tracks")
    from track2 in myXMLDoc.Root.Element("album").Element("tracks").Elements("track")
    where !track2.Element("name").Value.Contains(track.Element("Titel").Value)
    select track2;

query1 = query1.ToList();

//  Put a breakpoint HERE and examine query1 in the debugger to 
//  see what you got
;

foreach (var element in query1)
{
    cdXml.Element("Tracks").Add(
        new XElement("Artiest", element.Element("name").Value),
        new XElement("Titel", element.Element("artist").Element("name").Value),
        new XElement("Lengte", element.Element("duration").Value));
}

這是我的解決方案:

            XElement tracks =  cdXml.Root.Element("Tracks");

            List<XElement> query1 = myXMLDoc.Root.Element("album").Element("tracks").Elements("track")
                .Where(track2 => track2.Element("name").Value.Contains(track.Element("Titel").Value)).ToList();

            foreach (XElement q1 in query1)
            {
                tracks.Add(new XElement("Artiest", q1.Element("name").Value),
                         new XElement("Titel", q1.Element("artist").Element("name").Value),
                         new XElement("Lengte", q1.Element("duration").Value));
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM