Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:57:56+00:00 2026-06-03T06:57:56+00:00

XDocument xd = XDocument.Load(http://www.google.com/ig/api?weather=vilnius&hl=lt); The ampersand & isn’t a supported character in a string

  • 0
XDocument xd = XDocument.Load("http://www.google.com/ig/api?weather=vilnius&hl=lt");

The ampersand & isn’t a supported character in a string containing a URL when calling the Load() method. This error occurs:

XmlException was unhandled: Invalid character in the given encoding

How can you load XML from a URL into an XDocument where the URL has an ampersand in the querystring?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:57:58+00:00Added an answer on June 3, 2026 at 6:57 am


    You need to URL-encode it as &:

    XDocument xd = XDocument.Load(
        "http://www.google.com/ig/api?weather=vilnius&hl=lt");
    

    You might be able to get away with using WebUtility.HtmlEncode to perform this conversion automatically; however, be careful that this is not the intended use of that method.

    Edit: The real issue here has nothing to do with the ampersand, but with the way Google is encoding the XML document using a custom encoding and failing to declare it. (Ampersands only need to be encoded when they occur within special contexts, such as the <a href="…" /> element of (X)HTML. Read Ampersands (&’s) in URLs for a quick explanation.)

    Since the XML declaration does not specify the encoding, XDocument.Load is internally falling back to default UTF-8 encoding as required by XML specification, which is incompatible with the actual data.

    To circumvent this issue, you can fetch the raw data and decode it manually using the sample below. I don’t know whether the encoding really is Windows-1252, so you might need to experiment a bit with other encodings.

    string url = "http://www.google.com/ig/api?weather=vilnius&hl=lt";
    byte[] data;
    using (WebClient webClient = new WebClient())
        data = webClient.DownloadData(url);
    
    string str = Encoding.GetEncoding("Windows-1252").GetString(data);
    XDocument xd = XDocument.Parse(str);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code : string m_strFilePath = http://www.google.com/ig/api?weather=12414&hl=it; XmlDocument myXmlDocument = new XmlDocument();
i want get weather from http://www.google.com/ig/api?weather= I use this // load xml result from
What delay-load dependencies\functions are meant for in the following document? http://www.dependencywalker.com/faq.html Thanks
$q = $_GET['q']; // Load and parse the XML document $rss = simplexml_load_file('http://search.twitter.com/search.atom?lang=en&q=$q&rpp=100&page=1'); This
How can I load an external page (ex: http://www.google.pt ) to a div in
I have the following html <form action=http://www.google.com method=post name=first target=_blank id=first> <img src=loader.gif id=icon/>
greetings & salutations, this is my first run with the google maps API, i'm
So I have this plugin http://www.myjqueryplugins.com/jRating I downloaded it and tried it on a
I have the following code: <script type=text/javascript> $(document).ready(function() { $(#thang).load(http://www.yahoo.com); $(.SmoothLink).click( function() { $(#thang).fadeOut();
$(document).ready(function(){ $(a[href*='http://']:not([href*='+location.hostname+'])).attr(target,_blank); $(a[target!='_blank'][target!='_top']).click(function(){ $(#actualcontent).load($(this).attr(href)); window.location.hash=$(this).attr(href); return false; }); }); So I have this code

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.