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&domain=usenet">http://example.com/whoisgod?name=Kibo&domain=usenet</example>
so all ampersands are properly escaped as & (& 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&domain=usenet">http://example.com/whoisgod?name=Kibo&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/