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 exampleURLWithQ ueryString =
@"http://example.com/whoisgod?name=K ibo&domain=usen et";
XmlDocument xmlDocument = new XmlDocument();
xmlDocument.App endChild(xmlDoc ument.CreateEle ment("example") );
xmlDocument.Doc umentElement.Se tAttribute("lin k",
exampleURLWithQ ueryString);
xmlDocument.Doc umentElement.Ap pendChild(
xmlDocument.Cre ateTextNode(exa mpleURLWithQuer yString)
);
xmlDocument.Sav e(Console.Out);
will produce the markup
<example
link="http://example.com/whoisgod?name=K ibo&domain= usenet">http://example.com/whoisgod?name=K ibo&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 exampleURLWithQ ueryString =
@"http://example.com/whoisgod?name=K ibo&domain=usen et";
XmlTextWriter xmlWriter = new XmlTextWriter(C onsole.Out);
xmlWriter.Write StartElement("e xample");
xmlWriter.Write AttributeString ("link", exampleURLWithQ ueryString);
xmlWriter.Write String(exampleU RLWithQueryStri ng);
xmlWriter.Write EndElement();
xmlWriter.Close ();
gives the serialized markup
<example
link="http://example.com/whoisgod?name=K ibo&domain= usenet">http://example.com/whoisgod?name=K ibo&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/