469,282 Members | 2,359 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

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 InternetExplorer , 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
programmatically using C#. I was trying to use XmlDocument and
XmlValidatingReader 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.xml">] >
<root>
<element1>this is element 1</element1>
&inclX;
</root>

Example of "fragment.xml" content:
<element2>this is element 1</element2>
<element3>this is element 1</element3>
Nov 12 '05 #1
2 14258


Mark wrote:

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
programmatically using C#. I was trying to use XmlDocument and
XmlValidatingReader but I am getting exceptions at the moment when I expect
entity reference to be transformed into correct content.


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

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 12 '05 #2
I think I have just resolved the problem. Apparently the XML file was not
well formed and that is why XmlValidatingReader 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" );
XmlValidatingReader xr = new XmlValidatingReader( xtr );
xr.EntityHandling = EntityHandling.ExpandEntities;
xr.ValidationType = ValidationType.None;
while ( xr.Read() )
{
if ( xr.NodeType == XmlNodeType.Text )
{
Console.WriteLine( xmlReader.Value );
}
}

"Mark" <no****@nowhere.com> wrote in message
news:eV**************@TK2MSFTNGP10.phx.gbl...
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 InternetExplorer , 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
programmatically using C#. I was trying to use XmlDocument and
XmlValidatingReader 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.xml">] >
<root>
<element1>this is element 1</element1>
&inclX;
</root>

Example of "fragment.xml" content:
<element2>this is element 1</element2>
<element3>this is element 1</element3>

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Peter C. Chapin | last post: by
2 posts views Thread by Nick Craig-Wood | last post: by
1 post views Thread by Mark | last post: by
3 posts views Thread by bsagert | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.