Check wellformedness of XML

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

    #1

    Check wellformedness of XML

    Is there a way (without a schema or dtd) to check that my XML (currently
    held in a string variable) is wellformed? I've looked on google and in the
    help files and couldn't find anything about this.
    Thanks!

    Tom S


  • Dmitriy Lapshin [C# / .NET MVP]

    #2
    Re: Check wellformedness of XML

    Hi Tom,

    You can read the XML with XmlTextReader. To quote MSDN:

    "The XmlTextReader class is an implementation of XmlReader, and provides a
    fast, performant parser. It enforces the rules that XML must be well-formed.
    It is neither a validating nor a non-validating parser since it does not
    have DTD or schema information. It can read text in blocks, or read
    characters from a stream."

    And an example from another MSDN article to which I have added code to read
    the whole contents of the XML stream.

    string str = "<ROOT>AQID </ROOT>";
    XmlTextReader r = new XmlTextReader(n ew StringReader(st r));
    try
    {
    while (r.Read())
    {
    }
    }
    finally
    {
    r.Close();
    }

    --
    Sincerely,
    Dmitriy Lapshin [C# / .NET MVP]
    Bring the power of unit testing to the VS .NET IDE today!


    "Tom S" <nothing@nowher e.com> wrote in message
    news:%237cjrIn$ EHA.4072@TK2MSF TNGP10.phx.gbl. ..[color=blue]
    > Is there a way (without a schema or dtd) to check that my XML (currently
    > held in a string variable) is wellformed? I've looked on google and in
    > the help files and couldn't find anything about this.
    > Thanks!
    >
    > Tom S
    >[/color]

    Comment

    • Tom S

      #3
      Re: Check wellformedness of XML

      Great, thanks!

      "Dmitriy Lapshin [C# / .NET MVP]" <x-code@no-spam-please.hotpop.c om> wrote
      in message news:eRgfa2s$EH A.1296@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi Tom,
      >
      > You can read the XML with XmlTextReader. To quote MSDN:
      >
      > "The XmlTextReader class is an implementation of XmlReader, and provides a
      > fast, performant parser. It enforces the rules that XML must be
      > well-formed. It is neither a validating nor a non-validating parser since
      > it does not have DTD or schema information. It can read text in blocks, or
      > read characters from a stream."
      >
      > And an example from another MSDN article to which I have added code to
      > read the whole contents of the XML stream.
      >
      > string str = "<ROOT>AQID </ROOT>";
      > XmlTextReader r = new XmlTextReader(n ew StringReader(st r));
      > try
      > {
      > while (r.Read())
      > {
      > }
      > }
      > finally
      > {
      > r.Close();
      > }
      >
      > --
      > Sincerely,
      > Dmitriy Lapshin [C# / .NET MVP]
      > Bring the power of unit testing to the VS .NET IDE today!
      > http://www.x-unity.net/teststudio.aspx
      >
      > "Tom S" <nothing@nowher e.com> wrote in message
      > news:%237cjrIn$ EHA.4072@TK2MSF TNGP10.phx.gbl. ..[color=green]
      >> Is there a way (without a schema or dtd) to check that my XML (currently
      >> held in a string variable) is wellformed? I've looked on google and in
      >> the help files and couldn't find anything about this.
      >> Thanks!
      >>
      >> Tom S
      >>[/color]
      >[/color]


      Comment

      Working...