当我尝试加载XML file时,我收到错误:“根级别的数据无效。第1行,位置1”。这是相关的代码:
string line;
StreamReader file = new StreamReader(filepath);
while ((line = file.ReadLine()) != null)
{
XDocument doc = XDocument.Load("http://api.discogs.com/release/" + line);
//do processing
}
它的工作方式是,我在streamreader中打开一个包含版本号列表的文件,例如:
367315
123456
192837
然后我想逐个将它们附加到url的其余部分,以便我可以一次处理许多XML文件。但是,在加载XML时,我得到了上面提到的错误。我不知道是什么原因引起的。是良好的形式还是编码?我希望有人能帮助我!
答案 0 :(得分:4)
默认情况下,该网站返回Json ...以使其成为xml ....
string text;
using (WebClient client = new WebClient())
{
client.Headers["Accept"] = "application/xml";
text = client.DownloadString(@"http://api.discogs.com/release/" + line);
}
var elements = XElement.Parse(text);
var artists= elements.Descendants("artist")
.Select(e =>e.Element("role").Value
+ " "
+ e.Element("name").Value )
.ToList();
artists.ForEach(Console.WriteLine);
答案 1 :(得分:1)
您关联的文件的内容为
{"resp": {"status": true, "release": {"styles": ["Alternative Rock", "Pop
Rock"], "videos": [{"duration": 351, "description": "Coldplay a Rush of Bloo
d to the Head", "embed": true, "uri": "http://www.youtube.com/watch?
v=eoyyr1SFPCY", "title": "Coldplay a Rush of Blood to the Head"}], "series":
[], "labels": [{"name": "Parlophone", "entity_type": "1", "catno": "7243 5 40504 2
8", "resource_url": "http://api.discogs.com/labels/2294", "id":
2294, "entity_type_name": "Label"},
{"name": "Parlophone", "entity_type": "1", "catno": "540
5042", "resource_url": "http://api.discogs.com/labels/2294", "id": etc etc etc
所以它是一个JSON文件..使用JSONDeserializer来获取数据。
答案 2 :(得分:0)
返回的xml是一个片段。它没有根元素。您不能将XmlDocument.Load与片段一起使用。
This post应该指出正确的方向。
编辑:他的链接返回XML。这就是我从Fiddler那里得到的:
HTTP/1.1 200 OK
Reproxy-Status: yes
Content-Type: application/xml; charset=utf-8
Date: Wed, 25 Jan 2012 23:02:42 GMT
Server: lighttpd
Content-Length: 10574
Proxy-Connection: Keep-Alive
Connection: Keep-Alive
Age: 0
<resp stat="ok" version="2.0"><release id="367315" status="Accepted"><images><image height="296" type="secondary" uri="http://api.discogs.com/image/R-367315-1169408807.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1169408807.jpeg" width="600" /><image height="600" type="primary" uri="http://api.discogs.com/image/R-367315-1284410494.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1284410494.jpeg" width="600" /><image height="463" type="secondary" uri="http://api.discogs.com/image/R-367315-1284410524.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1284410524.jpeg" width="600" /><image height="591" type="secondary" uri="http://api.discogs.com/image/R-367315-1284410539.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1284410539.jpeg" width="598" /><image height="600" type="secondary" uri="http://api.discogs.com/image/R-367315-1284410555.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1284410555.jpeg" width="600" /><image height="600" type="secondary" uri="http://api.discogs.com/image/R-367315-1284410571.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1284410571.jpeg" width="600" /><image height="600" type="secondary" uri="http://api.discogs.com/image/R-367315-1284410583.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1284410583.jpeg" width="600" /><image height="600" type="secondary" uri="http://api.discogs.com/image/R-367315-1284410592.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1284410592.jpeg" width="600" /><image height="600" type="secondary" uri="http://api.discogs.com/image/R-367315-1284410604.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1284410604.jpeg" width="600" /><image height="600" type="secondary" uri="http://api.discogs.com/image/R-367315-1284410613.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1284410613.jpeg" width="600" /><image height="600" type="secondary" uri="http://api.discogs.com/image/R-367315-1284410625.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1284410625.jpeg" width="600" /><image height="463" type="secondary" uri="http://api.discogs.com/image/R-367315-1284410632.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1284410632.jpeg" width="600" /><image height="152" type="secondary" uri="http://api.discogs.com/image/R-367315-1284410641.jpeg" uri150="http://api.discogs.com/image/R-150-367315-1284410641.jpeg" width="599" /></images><artists><artist><id>29735</id><name>Coldplay</name><anv /><join /><role /><tracks /></artist></artists><title>A Rush Of Blood To The Head</title><labels><label catno="7243 5 40504 2 8" name="Parlophone" /><label catno="540 5042" name="Parlophone" /></labels><extraartists><artist><id>1840230</id><name>Dan Keeling (2)</name><anv /><join /><role>A&r</role><tracks /></artist><artist><id>257846</id><name>Audrey Riley</name><anv /><join /><role>Arranged By [String Arrangements]</role><tracks /></artist><artist><id>29735</id><name>Coldplay</name><anv /><join /><role>Arranged By [String Arrangements]</role><tracks /></artist><artist><id>471548</id><name>Blue Source</name><anv /><join /><role>Art Direction</role><tracks /></artist><artist><id>29735</id><name>Coldplay</name><anv /><join /><role>Art Direction</role><tracks /></artist><artist><id>2246115</id><name>Sølve Sundsbø</name><anv /><join /><role>Artwork [Cover Art]</role><tracks /></artist><artist><id>306452</id><name>Rik Simpson</name><anv /><join /><role>Engineer [Additional Engineering By]</role><tracks /></artist><artist><id>351549</id><name>Andrea Wright (2)</name><anv /><join /><role>Engineer [Assisted By]</role><tracks /></artist><artist><id>407167</id><name>Ben Thackeray</name><anv /><join /><role>Engineer [Assisted By]</role><tracks /></artist><artist><id>446281</id><name>Jon Bailey</name><anv /><join /><role>Engineer [Assisted By]</role><tracks /></artist><artist><id>1150797</id><name>Jon Withnall</name><anv>Jon Withnal</anv><join /><role>Engineer [Assisted By]</role><tracks /></artist><artist><id>251683</id><name>Ken Nelson</name><anv>Ken</anv><join /><role>Engineer [Engineered By]</role><tracks /></artist><artist><id>974611</id><name>Dave Holmes (7)</name><anv /><join /><role>Management</role><tracks /></artist><artist><id>1840237</id><name>Estelle Wilkinson</name><anv /><join /><role>Management</role><tracks /></artist><artist><id>1886107</id><name>Nettwerk Management</name><anv>Nettwerk</anv><join /><role>Management</role><tracks /></artist><artist><id>335003</id><name>George Marino</name><anv /><join /><role>Mastered By</role><tracks /></artist><artist><id>29735</id><name>Coldplay</name><anv /><join /><role>Mixed By</role><tracks /></artist><artist><id>113799</id><name>Danton Supple</name><anv /><join /><role>Mixed By</role><tracks>2, 3, 6, 8, 10</tracks></artist><artist><id>251683</id><name>Ken Nelson</name><anv /><join /><role>Mixed By</role><tracks /></artist><artist><id>251682</id><name>Mark Phythian</name><anv /><join /><role>Mixed By</role><tracks /></artist><artist><id>42610</id><name>Chris Martin</name><anv /><join /><role>Performer [Coldplay]</role><tracks /></artist><artist><id>530745</id><name>Guy Berryman</name><anv /><join /><role>Performer [Coldplay]</role><tracks /></artist><artist><id>530747</id><name>Jon Buckland</name><anv /><join /><role>Performer [Coldplay]</role><tracks /></artist><artist><id>530749</id><name>Phil Harvey</name><anv /><join /><role>Performer [Coldplay]</role><tracks /></artist><artist><id>530746</id><name>Will Champion</name><anv /><join /><role>Performer [Coldplay]</role><tracks /></artist><artist><id>2265953</id><name>Tom Sheehan (3)</name><anv /><join /><role>Photography By</role><tracks /></artist><artist><id>2247420</id><name>Zed Nelson</name><anv /><join /><role>Photography By</role><tracks /></artist><artist><id>29735</id><name>Coldplay</name><anv /><join /><role>Producer</role><tracks /></artist><artist><id>251683</id><name>Ken Nelson</name><anv /><join /><role>Producer</role><tracks /></artist><artist><id>251682</id><name>Mark Phythian</name><anv /><join /><role>Producer [Additional Production By]</role><tracks /></artist><artist><id>251683</id><name>Ken Nelson</name><anv>Ken</anv><join /><role>Recorded By</role><tracks /></artist><artist><id>251682</id><name>Mark Phythian</name><anv /><join /><role>Recorded By [With]</role><tracks /></artist><artist><id>42610</id><name>Chris Martin</name><anv>Martin</anv><join /><role>Songwriter</role><tracks /></artist><artist><id>530745</id><name>Guy Berryman</name><anv>Berryman</anv><join /><role>Songwriter</role><tracks /></artist><artist><id>530747</id><name>Jon Buckland</name><anv>Buckland</anv><join /><role>Songwriter</role><tracks /></artist><artist><id>530746</id><name>Will Champion</name><anv>Champion</anv><join /><role>Songwriter</role><tracks /></artist><artist><id>400611</id><name>Ann Lines</name><anv /><join /><role>Strings</role><tracks /></artist><artist><id>257846</id><name>Audrey Riley</name><anv /><join /><role>Strings</role><tracks /></artist><artist><id>274873</id><name>Chris Tombling</name><anv /><join /><role>Strings</role><tracks /></artist><artist><id>400609</id><name>Laura Melhuish</name><anv>Laura Melhewish</anv><join /><role>Strings</role><tracks /></artist><artist><id>303064</id><name>Leo Payne</name><anv /><join /><role>Strings</role><tracks /></artist><artist><id>273998</id><name>Peter Lale</name><anv /><join /><role>Strings</role><tracks /></artist><artist><id>400613</id><name>Richard George</name><anv /><join /><role>Strings</role><tracks /></artist><artist><id>273997</id><name>Sue Dench</name><anv>Susan Dench</anv><join /><role>Strings</role><tracks /></artist><artist><id>0</id><name>Berryman/Buckland/Champion/Martin</name><anv /><join /><role>Written By</role><tracks /></artist></extraartists><formats><format name="CD" qty="1"><descriptions><description>Album</description></descriptions></format></formats><genres><genre>Rock</genre></genres><styles><style>Alternative Rock</style><style>Pop Rock</style></styles><country>UK & Europe</country><released>2002-08-26</released><notes>Recorded at Studio 2 Mayfair, Studio 3 Parr Street and Studio 1 Air between September 2001 and May 2002.
Mastered at Sterling Sound.
The Scientist is Dan.
℗ 2002 The copyright of this sound is owned by EMI Records Ltd. © 2002 EMI Records Ltd.
[On back cover:] Published by BMG Music Publishing Ltd. Printed in the EU. Marketed and distributed by EMI.
[On CD label:] Made in EU.
This CD is Carbon Neutral courtesy of Future Forests
</notes><master_id>3354</master_id><data_quality>Correct</data_quality><tracklist><track><position>1</position><title>Politik</title><duration>5:18</duration></track><track><position>2</position><title>In My Place</title><duration>3:48</duration></track><track><position>3</position><title>God Put A Smile Upon Your Face</title><duration>4:57</duration></track><track><position>4</position><title>The Scientist</title><duration>5:09</duration></track><track><position>5</position><title>Clocks</title><duration>5:07</duration></track><track><position>6</position><title>Daylight</title><duration>5:27</duration></track><track><position>7</position><title>Green Eyes</title><duration>3:43</duration></track><track><position>8</position><title>Warning Sign</title><duration>5:31</duration></track><track><position>9</position><title>A Whisper</title><duration>3:58</duration></track><track><position>10</position><title>A Rush Of Blood To The Head</title><duration>5:51</duration></track><track><position>11</position><title>Amsterdam</title><duration>5:19</duration></track></tracklist><identifiers><identifier description="Text" type="Barcode" value="7 24354 05042 8" /><identifier description="Scanned" type="Barcode" value="724354050428" /><identifier type="Rights Society" value="BEL BIEM" /><identifier description="Distribution Code, France" type="Other" value="PM 520" /><identifier type="Label Code" value="LC0299" /><identifier description="Variant 1" type="Matrix / Runout" value="5405042" /><identifier description="Variant 2" type="Matrix / Runout" value="5405042 A838865-01 manufactured by optimal media production" /><identifier description="Mould SID Code, Variant 2" type="Other" value="IFPI 9712" /><identifier description="Mastering SID Code, Variant 2" type="Other" value="IFPI L573" /></identifiers><videos><video duration="351" embed="true" src="http://www.youtube.com/watch?v=eoyyr1SFPCY"><title>Coldplay a Rush of Blood to the Head</title><description>Coldplay a Rush of Blood to the Head</description></video></videos></release></resp>
哪个是xml片段。因此,您可以使用XElement.Load,而不是使用XDocument.Load(抱歉让我以前的回复与XmlDocument.Load混淆)。请参阅this wonderful answer。