Is there XmlDecode/XmlEncode?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fallen

    #1

    Is there XmlDecode/XmlEncode?

    I have a string containing XML embedded withing another XML document
    as an attribute value. This is what the string looks like:
    <message> quick brown fox&ltmessage/>

    I want to convert this to: <message>quic k brown fox</message>

    How?
    (I don't think XmlConvert handles this.)

    regards,
    T

  • John Saunders [MVP]

    #2
    Re: Is there XmlDecode/XmlEncode?

    "Fallen" <sw.tejasvi.sn@ gmail.comwrote in message
    news:1179989178 .951052.47690@b 40g2000prd.goog legroups.com...
    >I have a string containing XML embedded withing another XML document
    as an attribute value. This is what the string looks like:
    &lt;message&gt; quick brown fox&ltmessage/&gt;
    >
    I want to convert this to: <message>quic k brown fox</message>
    >
    How?
    What happens if you do this:

    XmlDocument doc = new XmlDocument();
    doc.LoadXml(myW holeDocument);
    // Assuming <child attributeWithXM L="&lt;message& gt;quick brown
    fox&ltmessage/&gt;"/>
    string embeddedXml =
    doc.DocumentEle ment["child"].Attributes["attributeWithX ML"].Value;
    Console.WriteLi ne(embeddedXml) ;

    What prints out?
    --
    John Saunders [MVP]


    Comment

    • T

      #3
      Re: Is there XmlDecode/XmlEncode?

      Thanks John,

      The following code does indeed print <message>quic k brown fox</
      message>.
      But I know exactly where in the containing XML this string occurs. I
      am able to extract this without the expense of parsing. I am looking
      for a shortcut to unescape the embedded XML. But thanks the answer is
      obvious from here.

      Code:
      using System;
      using System.Xml;
      
      class Starter {
      public static void Main(string[] args){
      XmlDocument doc = new XmlDocument();
      string xml = "<d><child attributeWithXML=\"&lt;message&gt;quick
      brown fox&lt;/message&gt;\" /></d>";
      doc.LoadXml(xml);
      string embeddedXml =
      doc.DocumentElement["child"].Attributes["attributeWithXML"].Value;
      Console.WriteLine(embeddedXml);
      }
      }
      On May 24, 4:11 pm, "John Saunders [MVP]" <john.saunder s at
      trizetto.comwro te:
      "Fallen" <sw.tejasvi...@ gmail.comwrote in message
      >
      news:1179989178 .951052.47690@b 40g2000prd.goog legroups.com...
      >
      I have a string containing XML embedded withing another XML document
      as an attribute value. This is what the string looks like:
      &lt;message&gt; quick brown fox&ltmessage/&gt;
      >
      I want to convert this to: <message>quic k brown fox</message>
      >
      How?
      >
      What happens if you do this:
      >
      XmlDocument doc = new XmlDocument();
      doc.LoadXml(myW holeDocument);
      // Assuming <child attributeWithXM L="&lt;message& gt;quick brown
      fox&ltmessage/&gt;"/>
      string embeddedXml =
      doc.DocumentEle ment["child"].Attributes["attributeWithX ML"].Value;
      Console.WriteLi ne(embeddedXml) ;
      >
      What prints out?
      --
      John Saunders [MVP]

      Comment

      • John Saunders [MVP]

        #4
        Re: Is there XmlDecode/XmlEncode?

        "T" <sw.tejasvi.sn@ gmail.comwrote in message
        news:1180007476 .237216.35420@h 2g2000hsg.googl egroups.com...
        Thanks John,
        >
        The following code does indeed print <message>quic k brown fox</
        message>.
        But I know exactly where in the containing XML this string occurs. I
        am able to extract this without the expense of parsing. I am looking
        for a shortcut to unescape the embedded XML. But thanks the answer is
        obvious from here.
        Glad to help.

        The XmlDocument example was just a simple example to illustrate the fact
        that the XML API's know how to escape and unescape XML. If you're only
        looking for the one string, then an XPathDocument or even a simple XmlReader
        would be much faster. Both of them understand how strings need to be
        escaped.
        --
        John Saunders [MVP]


        Comment

        Working...