Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > XML Escape Code Question for entering Ampersand

Reply
Thread Tools

XML Escape Code Question for entering Ampersand

 
 
needin4mation@gmail.com
Guest
Posts: n/a
 
      06-20-2005
Hi,

string filename = "Directions2.xml";
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("Directions2.xml"));
XmlElement root = doc.DocumentElement;
XmlNode oldLoc;
oldLoc =
root.SelectSingleNode("/NewDataSet/Locations[LocID='mainstreetD']");
XmlElement newLoc = doc.CreateElement("Locations");
newLoc.InnerXml="<LocID>" + txtName.Text + "</LocID>" +
"<Phone>" + txtPhone.Text + "</Phone>" +
"<Address>" + txtDirections.Text + "</Address>";
root.ReplaceChild(newLoc,oldLoc);

I am just trying to figure out how to make it so the user can enter an
ampersand & into the textbox for an update.

Thank you for any ideas.

 
Reply With Quote
 
 
 
 
Hans Kesting
Guest
Posts: n/a
 
      06-20-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi,
>
> string filename = "Directions2.xml";
> XmlDocument doc = new XmlDocument();
> doc.Load(Server.MapPath("Directions2.xml"));
> XmlElement root = doc.DocumentElement;
> XmlNode oldLoc;
> oldLoc =
> root.SelectSingleNode("/NewDataSet/Locations[LocID='mainstreetD']");
> XmlElement newLoc = doc.CreateElement("Locations");
> newLoc.InnerXml="<LocID>" + txtName.Text + "</LocID>" +
> "<Phone>" + txtPhone.Text + "</Phone>" +
> "<Address>" + txtDirections.Text + "</Address>";
> root.ReplaceChild(newLoc,oldLoc);
>
> I am just trying to figure out how to make it so the user can enter an
> ampersand & into the textbox for an update.
>
> Thank you for any ideas.


If you use an XmlTextWriter to build your xml-string, that translation is done
for you, and also for ">" and "<".

using System.IO;
using System.Text;
using System.Xml;

StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter xw = new XmlTextWriter(sw);

// now add xml with the various methods of "xw"

newLoc.InnerXml = sb.ToString();



Hans Kesting


 
Reply With Quote
 
 
 
 
needin4mation@gmail.com
Guest
Posts: n/a
 
      06-20-2005
So are you saying to take this:

="<LocID>" + txtName.Text + "</LocID>" +
"<Phone>" + txtPhone.Text + "</Phone>" +
"<Address>" + txtDirections.Text + "</Address>";

and put that in the StringBuilder sb and then assign sb the
xw.WriteElementString to it?

(and it didn't work for me, but, for example)
String myVar = txtName.text;
sb = xw.WriteElementString("LocID", myVar);
newLoc.InnerXml = sb.ToString();

? Thanks again.

 
Reply With Quote
 
needin4mation@gmail.com
Guest
Posts: n/a
 
      06-21-2005
Is there any automated way to get the ampersand out?

 
Reply With Quote
 
Hans Kesting
Guest
Posts: n/a
 
      06-21-2005
(E-Mail Removed) wrote:
> So are you saying to take this:
>
> ="<LocID>" + txtName.Text + "</LocID>" +
> "<Phone>" + txtPhone.Text + "</Phone>" +
> "<Address>" + txtDirections.Text + "</Address>";
>
> and put that in the StringBuilder sb and then assign sb the
> xw.WriteElementString to it?
>
> (and it didn't work for me, but, for example)
> String myVar = txtName.text;
> sb = xw.WriteElementString("LocID", myVar);
> newLoc.InnerXml = sb.ToString();
>
> ? Thanks again.


No, the "WriteElementString" writes a *single* element (tags + enclosed text)
so you need three calls:

-----
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
XmlTextWriter xw = new XmlTextWriter(sw);

xw.WriteElementString("LocID", txtName.Text);
xw.WriteElementString("Phone", txtPhone.Text);
xw.WriteElementString("Address", txtDirections.Text);

newLoc.InnerXml = sb.ToString();
------
You never write to the "sb", you only read the contents at the end.
You also never need to touch "sw".

Hans Kesting



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read strings cantaining escape character from a file and useit as escape sequences? slomo Python 5 12-02-2007 11:39 AM
GUI for entering XML data emes XML 0 01-21-2006 10:07 PM
Preventing control characters from entering an XML file Frank Niessink Python 3 01-06-2006 12:58 AM
Ignoring ampersand(&) as a special character in xml Thierry Lam Python 2 09-06-2005 07:58 PM
Ampersand in attribute tfortney ASP .Net 4 04-28-2004 10:22 AM



Advertisments