how to read XML with entities using C#

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

    how to read XML with entities using C#

    Hi there,
    I have two xml files, one is a master file and the other is just a fragment
    of xml. Master xml file uses 'DOCTYPE' to define the other file as an
    entity. Then, the master uses entity references that are supposed to be
    expanded into real content at parsing time. Examples are provided below.
    When I open master xml file in InternetExplore r , IE shows correct content.
    All the entities are transformed into right xml. So far I have been
    unsuccessfull in getting similar result when I try to do the same
    programmaticall y using C#. I was trying to use XmlDocument and
    XmlValidatingRe ader but I am getting exceptions at the moment when I expect
    entity reference to be transformed into correct content.

    Is there a generic way [in C#] of expanding all the entities into real
    content so when I read my xml I can get the content, not exceptions. Is it
    possible at all ? If so, could anyone point me to an example that deals
    with this issue ?

    Thank you,
    Mark.

    Example of master file:
    <!DOCTYPE inclusion [<!ENTITY inclX SYSTEM "fragment.x ml">] >
    <root>
    <element1>thi s is element 1</element1>
    &inclX;
    </root>

    Example of "fragment.x ml" content:
    <element2>thi s is element 1</element2>
    <element3>thi s is element 1</element3>


  • Martin Honnen

    #2
    Re: how to read XML with entities using C#



    Mark wrote:

    [color=blue]
    > All the entities are transformed into right xml. So far I have been
    > unsuccessfull in getting similar result when I try to do the same
    > programmaticall y using C#. I was trying to use XmlDocument and
    > XmlValidatingRe ader but I am getting exceptions at the moment when I expect
    > entity reference to be transformed into correct content.[/color]

    For XmlDocument you need to set the XmlResolver property in your code.



    --

    Martin Honnen --- MVP XML

    Comment

    • Mark

      #3
      Re: how to read XML with entities using C#

      I think I have just resolved the problem. Apparently the XML file was not
      well formed and that is why XmlValidatingRe ader was blowing with exception.
      I apologize for posting the question without applying enough testing first.
      Mark

      For those who are interested, here is the example that parses perfectly my
      xml files:

      XmlTextReader xtr = new XmlTextReader(@ "d:\temp\master .xml" );
      XmlValidatingRe ader xr = new XmlValidatingRe ader( xtr );
      xr.EntityHandli ng = EntityHandling. ExpandEntities;
      xr.ValidationTy pe = ValidationType. None;
      while ( xr.Read() )
      {
      if ( xr.NodeType == XmlNodeType.Tex t )
      {
      Console.WriteLi ne( xmlReader.Value );
      }
      }



      "Mark" <nospam@nowhere .com> wrote in message
      news:eV319v7hFH A.1372@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Hi there,
      > I have two xml files, one is a master file and the other is just a
      > fragment of xml. Master xml file uses 'DOCTYPE' to define the other file
      > as an entity. Then, the master uses entity references that are supposed
      > to be expanded into real content at parsing time. Examples are provided
      > below. When I open master xml file in InternetExplore r , IE shows correct
      > content. All the entities are transformed into right xml. So far I have
      > been unsuccessfull in getting similar result when I try to do the same
      > programmaticall y using C#. I was trying to use XmlDocument and
      > XmlValidatingRe ader but I am getting exceptions at the moment when I
      > expect entity reference to be transformed into correct content.
      >
      > Is there a generic way [in C#] of expanding all the entities into real
      > content so when I read my xml I can get the content, not exceptions. Is it
      > possible at all ? If so, could anyone point me to an example that deals
      > with this issue ?
      >
      > Thank you,
      > Mark.
      >
      > Example of master file:
      > <!DOCTYPE inclusion [<!ENTITY inclX SYSTEM "fragment.x ml">] >
      > <root>
      > <element1>thi s is element 1</element1>
      > &inclX;
      > </root>
      >
      > Example of "fragment.x ml" content:
      > <element2>thi s is element 1</element2>
      > <element3>thi s is element 1</element3>
      >
      >[/color]


      Comment

      Working...