Encoding with HttpClient in .NET 4.5

前端 未结 2 1974
小鲜肉
小鲜肉 2020-12-19 02:47

I\'m consuming some data using the fogbugz XML API. This API always offers data as UTF-8.

When using the WebClient class for making a request I am able

2条回答
  •  再見小時候
    2020-12-19 03:11

    If I understand correctly, you don't need a string, you need XML.

    So, assuming your data is not too big, read a byte array with

    byte[] bytes = await client.GetByteArrayAsync(url); 
    

    then create a memory stream from that array, and then read XML from that stream, for example:

    XElement element = XElement.Load(new MemoryStream(bytes), LoadOptions.None);
    

    If you're using another XML API, you can use

    XmlReader reader = XmlReader.Create(new MemoryStream(bytes));
    

提交回复
热议问题