By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,978 Members | 1,831 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,978 IT Pros & Developers. It's quick & easy.

Problem with URL characters to be saved in XML formatted doc.

P: n/a
Joe
I want to save some URLs into a XML formatted document.

I find out that its having some problems due to some of the characters
used
in the URL.

Is there a quick way to get around that? Thanks.

Here are some of the URL characters in the parameters

www.site.com/='N+Ss+Volume'&sym=

http://www2.site.com/

http://www.site.com/.?scanType=all_s...ange=all_&x=14


Are there any easy to use prewritten code to take care of this (the special
characters used in the URL I need to store in the XML formatted are causing
load and read problems with the XMLdocument object)?
Feb 5 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a


Joe wrote:
I want to save some URLs into a XML formatted document. Are there any easy to use prewritten code to take care of this (the special
characters used in the URL I need to store in the XML formatted are causing
load and read problems with the XMLdocument object)?


The character & needs to be escaped as & (& a m p ; for web forum
readers) but any of the XML tools will do that automatically if you put
it into a text node/content of an element or attribute e.g. the
following C# code

string exampleURLWithQueryString =
@"http://example.com/whoisgod?name=Kibo&domain=usenet";

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.AppendChild(xmlDocument.CreateElement( "example"));
xmlDocument.DocumentElement.SetAttribute("link",
exampleURLWithQueryString);
xmlDocument.DocumentElement.AppendChild(
xmlDocument.CreateTextNode(exampleURLWithQueryStri ng)
);

xmlDocument.Save(Console.Out);

will produce the markup

<example
link="http://example.com/whoisgod?name=Kibo&amp;domain=usenet">http://example.com/whoisgod?name=Kibo&amp;domain=usenet</example>

so all ampersands are properly escaped as &amp; (& a m p ; for web readers).

Same if you use an XmlTextWriter e.g. the C# code

string exampleURLWithQueryString =
@"http://example.com/whoisgod?name=Kibo&domain=usenet";

XmlTextWriter xmlWriter = new XmlTextWriter(Console.Out);
xmlWriter.WriteStartElement("example");
xmlWriter.WriteAttributeString("link", exampleURLWithQueryString);
xmlWriter.WriteString(exampleURLWithQueryString);
xmlWriter.WriteEndElement();
xmlWriter.Close();
gives the serialized markup

<example
link="http://example.com/whoisgod?name=Kibo&amp;domain=usenet">http://example.com/whoisgod?name=Kibo&amp;domain=usenet</example>

Thus if you use the proper tools to create your XML then there should
not be a problem.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Feb 5 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.