你好我了解这个东西我需要发送请求到网站获取xml响应而不是反序列化它,看看里面有什么...我创建了请求deserialize方法和流方法和Xml架构但现在我不知道是什么接下来它就会工作,所以如果有人知道一些不错的教程请给我链接。
public static class LoadXml
{
public static root material;
public static void LoadXML()
{
var serviceUrl = "http://api.deezer.com/2.0/artist/27&output=xml";
string serviceName = "Deezer";
HttpWebRequest request = null;
WebResponse response = null;
request = WebRequest.Create(serviceUrl) as HttpWebRequest;
request.Method = "GET";
request.ContentType = " text/xml";
material = Deserialize<root>(GetResponseStream(request, response, serviceName));
Console.WriteLine(material.ToString());
}
public static T Deserialize<T>(MemoryStream stream)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
T result = (T)serializer.Deserialize(stream);
return result;
}
public static MemoryStream GetResponseStream(HttpWebRequest request, WebResponse response, string debugServiceName)
{
response = request.GetResponse();
MemoryStream stream = new MemoryStream();
response.GetResponseStream().CopyTo(stream);
stream.Position = 0;
return stream;
}
}
答案 0 :(得分:1)
将结果放入(或加载)到[XDocument] [1]并通过操作和提取文档中的数据从那里开始工作。
使用url作为起点,这是一个片段(更容易加载;尝试它)我们加载它然后查看目标子节点,如果返回的结构是(\ root \ name):
XDocument doc = XDocument.Load(@"http://api.deezer.com/2.0/artist/27&output=xml");
Console.WriteLine ( doc.ToString() );
/*
<root>
<id><![CDATA[27]]></id>
<name><![CDATA[Daft Punk]]></name>
<link><![CDATA[http://www.deezer.com/artist/27]]></link>
<picture><![CDATA[http://api.deezer.com/2.0/artist/27/image]]></picture>
<nb_album><![CDATA[54]]></nb_album>
<nb_fan><![CDATA[592180]]></nb_fan>
<radio><![CDATA[1]]></radio>
<type><![CDATA[artist]]></type>
*/
Console.WriteLine ( doc.Element("root").Element("name").Value);
// Outputs:
// Daft Punk
var xml = @"
<root>
<id><![CDATA[27]]></id>
<name><![CDATA[Daft Punk]]></name>
<link><![CDATA[http://www.deezer.com/artist/27]]></link>
<picture><![CDATA[http://api.deezer.com/2.0/artist/27/image]]></picture>
<nb_album><![CDATA[54]]></nb_album>
<nb_fan><![CDATA[592180]]></nb_fan>
<radio><![CDATA[1]]></radio>
<type><![CDATA[artist]]></type>
</root>";
var doc = XDocument.Parse( xml );
Console.WriteLine ( doc.Element("root").Element("name").Value);
// Outputs
// Daft Punk