replace invalid xml characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John A Grandy

    replace invalid xml characters

    I know that System.Security .SecurityElemen t.Escape() will replace invalid
    xml chars with valid equivalent ...

    But is there another method to accomplish same that is "closer to home" ?


  • Mr Flibble

    #2
    Re: replace invalid xml characters

    * John A Grandy wrote:[color=blue]
    > I know that System.Security .SecurityElemen t.Escape() will replace invalid
    > xml chars with valid equivalent ...
    >
    > But is there another method to accomplish same that is "closer to home" ?
    >
    >[/color]

    I usually use the stream editor "sed" for this type of stuff.

    Comment

    • Martin Honnen

      #3
      Re: replace invalid xml characters



      John A Grandy wrote:
      [color=blue]
      > I know that System.Security .SecurityElemen t.Escape() will replace invalid
      > xml chars with valid equivalent ...
      >
      > But is there another method to accomplish same that is "closer to home" ?[/color]

      If you want to create XML with .NET then Xml(Text)Writer is the tool and
      its methods will do all escaping necessary for you e.g.

      XmlTextWriter xmlWriter = new XmlTextWriter(C onsole.Out);
      xmlWriter.Forma tting = Formatting.Inde nted;
      xmlWriter.Write StartDocument() ;
      xmlWriter.Write StartElement("r oot");
      xmlWriter.Write ElementString(" text", "Kibo & Xibo");
      xmlWriter.Write ElementString(" comparison", "a < b");
      xmlWriter.Write EndDocument();
      xmlWriter.Close ();

      will write

      <root>
      <text>Kibo &amp; Xibo</text>
      <comparison>a &lt; b</comparison>
      </root>

      so everything is escaped as necessary.


      If that does not help then tell exactly which kind of "invalid"
      characters you have or maybe check first whether some of the other methods
      <http://msdn.microsoft. com/library/default.asp?url =/library/en-us/cpref/html/frlrfSystemXmlX mlTextWriterMet hodsTopic.asp>
      Xml(Text)Writer provides like WriteBase64 or WriteBinHex do what you need.



      --

      Martin Honnen --- MVP XML

      Comment

      Working...