XML Escape Code Question for entering Ampersand

Discussion in 'ASP .Net' started by needin4mation@gmail.com, Jun 20, 2005.

  1. Guest

    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.
     
    , Jun 20, 2005
    #1
    1. Advertising

  2. Hans Kesting Guest

    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
     
    Hans Kesting, Jun 20, 2005
    #2
    1. Advertising

  3. Guest

    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.
     
    , Jun 20, 2005
    #3
  4. Guest

    Is there any automated way to get the ampersand out?
     
    , Jun 21, 2005
    #4
  5. Hans Kesting Guest

    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
     
    Hans Kesting, Jun 21, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael
    Replies:
    2
    Views:
    6,260
    Michael
    Nov 9, 2005
  2. emes
    Replies:
    0
    Views:
    421
  3. Thierry Lam
    Replies:
    2
    Views:
    382
    Chris
    Sep 6, 2005
  4. Frank Niessink
    Replies:
    3
    Views:
    365
    Scott David Daniels
    Jan 6, 2006
  5. slomo
    Replies:
    5
    Views:
    1,549
    Duncan Booth
    Dec 2, 2007
Loading...

Share This Page