Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am trying to read a XML file from a URL and below is my code in .net core

var url = "https://some.com/feeds/newfile.xml";
           var httpClient = new HttpClient();
           var result = httpClient.GetAsync(url).Result;
           var stream = result.Content.ReadAsStreamAsync().Result;

           var itemXml = XElement.Load(stream);



when the deubgger hits at .load , it throws an error "error on line 22 at column 46: xmlParseEntityRef: no name"


Below is the snapshot of the XML where error occurs ie line 22

<li>Win management’s support for your LOTO & machine guarding initiatives  </li>


What I have tried:

I've tried disabling the error or syntax check but no success
Posted
Updated 23-Jul-18 4:13am
Comments
Richard Deeming 23-Jul-18 10:15am    
The URL you're trying to load is not returning valid XML. The & character needs to be encoded as &amp;.
Abhi1 Kanobi 23-Jul-18 10:19am    
Thank you, for your response.

Any way I can encode it before it loads?

1 solution

The problem is around & - it is a special character in XML and should be treated as such...
In simple words - the XML is invalid...
List of XML and HTML character entity references - Wikipedia[^]
 
Share this answer
 
Comments
Abhi1 Kanobi 23-Jul-18 10:19am    
Thank you, for your response.

Any way I can encode it before it loads?
Kornfeld Eliyahu Peter 23-Jul-18 14:36pm    
Actually it is the responsibility of the site you are downloading from...
However if it can not be resolved, you may try to download the content as text first, than encode it and load into XML...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900