namespace ConsoleApp1
{
class Program
{
static HttpClient client = new HttpClient();
static void Main(string[] args)
{
RunAsync().Wait();
}
static async Task<string> GetProductAsync(string path = "https://www.fourmilab.ch/cgi-bin/Hotbits.api?nbytes=8&fmt=xml&npass=1&lpass=8&pwtype=3&apikey=HB18CsHhr5Muzoee1KAu4QY5xUe")
{
string product = string.Empty;
HttpResponseMessage response = await client.GetAsync(path);
if (response.IsSuccessStatusCode)
{
product = await response.Content.ReadAsAsync<string>();
}
return product;
}
static async Task RunAsync()
{
string str = await GetProductAsync();
XmlDocument xml = new XmlDocument();
xml.Load(str);
Console.WriteLine(xml.InnerXml);
XmlNodeList list = xml.GetElementsByTagName("random-data");
string[] strs = list[0].InnerXml.Split(' ');
foreach (object e in strs)
{
Console.WriteLine(e);
}
}嗨,这是我在c#中调用hotbits的源代码,我得到的错误是
"CData元素在XML文档的顶层无效。第1行,位置3“
如果有人能帮忙,这是非常感谢的。
发布于 2017-11-01 18:22:03
使用Xml尝试如下:
const string URL = "https://www.fourmilab.ch/cgi-bin/Hotbits.api?nbytes=8&fmt=xml&npass=1&lpass=8&pwtype=3&apikey=HB18CsHhr5Muzoee1KAu4QY5xUe";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(URL);
string randomNumbers = (string)doc.Descendants("random-data").FirstOrDefault();
int[] numbers = randomNumbers.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x, System.Globalization.NumberStyles.HexNumber)).ToArray();
Console.WriteLine(string.Join(",",numbers.Select(x => x.ToString())));
Console.ReadLine();
}https://stackoverflow.com/questions/47060991
复制相似问题