C# 将现有xml元素添加到xml文档中
在这个LINQ查询中,我正在为自己的CD创建一个新的XDocument,其中包含跟踪对象:C# 将现有xml元素添加到xml文档中,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,在这个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
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>
首先,编写一个查询,返回要添加到第二个文档中的元素。就这样。编写一个返回这些信息的查询。在调试器中,确认您得到了想要得到的 然后,编写一个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));
}
它不起作用:-你希望代码做什么,它做什么呢?我想将myXMLDoc Artister、name、duration中的xml元素添加到cdXml文档中,以便它将:Dream Theater标记和内容添加到其中。它做了什么呢?我现在得到了一个NullReferenceException@meesie1所以,找出什么是空的。您没有提供任何XML,因此我无法测试。@meesie1您是否使用调试器查找query1中的内容?您从哪里获得NullReferenceException?哪里什么线路?唤醒梦幻剧场——最后一次被网络捕获。fm/music/Dream+Theater/u/catch+in+a+web3280-Dream Theater 28503ab7-8bb2-4666-a7bd-2644bfc7cb1d last。fm/music/Dream+Theater@meesie1请把这个问题放在你的问题里。你不能把它放在评论里。你不能。评论太少了。评论中没有足够的空间。XML太大,无法容纳。
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));
}