XML Escape Code Question for entering Ampersand

N

needin4mation

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.
 
H

Hans Kesting

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
 
N

needin4mation

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.
 
H

Hans Kesting

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();
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top