Monday, February 20, 2012

Problem inserting XML with DTD

Hi,
I am trying to insert a XML document into a SQL-Server 2005 column of type
XML:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE ctobject PUBLIC "-//my Ges.m.b.H.//DTD Procedural//EN"
"proced.dtd">
<ctobject>
<content>Some data with entity ref ü</content>
</ctobject>
The C#-Code for the operation is:
SqlConnection conn = new SqlConnection(connString);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "Insert XmlData(XmlData) Values(@.Col1)";
SqlParameter p1 = cmd.Parameters.Add("@.Col1", SqlDbType.Xml);
XmlTextReader reader = new XmlTextReader(fn);
reader.XmlResolver = resolver;
p2.Value = new SqlXml(reader);
cmd.ExecuteNonQuery();
The operation produces an "InvalidOperationException" - "DTD is not allowed
in XML fragments" on instantiation of the "SqlXml" type.
Removing the DOCTYPE reference eliminates the problem but of course produces
another one: "XmlException" - "Reference to undeclared entity" (caused by
the "ü" entity reference).
What can I do.
Thanks in advance,
ErwinHello Erwin,
Since DTDs are not supported by SQL Server (but XML Schemas are :-) ) you
will have to replace the ü with something else (like <uuml/>, ü or
) and then replace it again on the client side (with ü) when you fetch
it back to the client.
You should however consider not using DTDs.
HTH
/Tobias|||Hello Tobias,
thank you for the response.
Unfortunately not using DTDs is currently not an option (large set of
technical documentation from customer, who is not prepared for switching to
schemas).
So if we decide to work with SQL-Server we will need some kind of
workaround:
usage of partial schemas for indexing (performance) purposes
storage of XML with resolved entities
...
Anyway, a very important requirement is to reproduce an identical copy of
the original document on store/retrieve operations. We will therefore need
some type of replace/restore operations (the way you mentioned in your
response). As an alternative we could try to store a copy of the original
XML to a simple "varchar" column. The first solution has the divantage of
producing extra computational overhead. It also ev. manipulates the original
document structure (ev. infuencing search operations). The second solution
produces redundant data with possible problems on data modification
activities.
Kind regards,
Erwin
"Tobias Thernstrm" <ttnospam@.rbam.nospam.se> schrieb im Newsbeitrag
news:ufcBW6pbGHA.4716@.TK2MSFTNGP03.phx.gbl...
> Hello Erwin,
> Since DTDs are not supported by SQL Server (but XML Schemas are :-) ) you
> will have to replace the ü with something else (like <uuml/>, ü
> or ) and then replace it again on the client side (with ü) when you
> fetch it back to the client.
> You should however consider not using DTDs.
> HTH
> /Tobias
>

No comments:

Post a Comment