Home All Groups Group Topic Archive Search About

WriteXML encoding strings ! Please help

Author
20 Nov 2004 8:32 AM
sorCrer
Hi Guru's,

Going crazy with this problem!

I fill a dataset in asp.net with a 'FOR XML AUTO' query and Output it
back to the browser.

> DS.WriteXml(Response.OutputStream, XmlWriteMode.IgnoreSchema)

No matter what I do I get the first nodes "<" encoded as "&lt;" This
means that the script that I am using can't recognise the first node.
I would replace the characters but then the output is no longer
recognised as XML.

Why is this behaviour implemented? I basically end with this

<Tables>
    <XML_F52E2B61-18A1-11d1-B105-00805F49916B>&lt;row tID="1" tNo="11"
tLeft="123" tTop="432"/&gt;</XML_F52E2B61-18A1-11d1-B105-00805F49916B>
</Tables>

Please assist urgent!

Thanks
sorCrer

Author
21 Nov 2004 9:45 AM
Oleg Tkachenko [MVP]
sorCrer wrote:

> I fill a dataset in asp.net with a 'FOR XML AUTO' query and Output it
> back to the browser.
>
>
>>DS.WriteXml(Response.OutputStream, XmlWriteMode.IgnoreSchema)
>
>
> No matter what I do I get the first nodes "<" encoded as "&lt;" This
> means that the script that I am using can't recognise the first node.

> Why is this behaviour implemented?

In XML < characters in text and attribute values must be escaped.

> I basically end with this
>
> <Tables>
>     <XML_F52E2B61-18A1-11d1-B105-00805F49916B>&lt;row tID="1" tNo="11"
> tLeft="123" tTop="432"/&gt;</XML_F52E2B61-18A1-11d1-B105-00805F49916B>
> </Tables>

What's you start with?
Looks like you start with embedded XML as a character data? That's the
problem - you've got it as text and so DataSet writes it as text,
escaping < and &.
So the solution is either to change the way you store it or disable
escaping (e.g. using XSLT or custom XmlWriter).

--
Oleg Tkachenko [XML MVP]
http://blog.tkachenko.com
Author
21 Nov 2004 10:17 PM
Nigel Armstrong
Hi SorCrer

How are you getting the XML back from your query?

You need to use the ExecuteXmlReader method on your SqlCommand object...

This works OK for me...

        Dim conn As New System.Data.SqlClient.SqlConnection("connString")
        Dim comm As New System.Data.SqlClient.SqlCommand("SELECT * FROM
Table FOR XML AUTO", conn)
        conn.Open()
        Dim r As System.Xml.XmlReader = comm.ExecuteXmlReader()
        Dim ds As New DataSet
        ds.ReadXml(r)
        r.close()
        conn.Close()
        MessageBox.Show(ds.GetXml())

Let me know if you need more help

Nigel Armstrong

Show quote
"sorCrer" wrote:

> Hi Guru's,
>
> Going crazy with this problem!
>
> I fill a dataset in asp.net with a 'FOR XML AUTO' query and Output it
> back to the browser.
>
> > DS.WriteXml(Response.OutputStream, XmlWriteMode.IgnoreSchema)
>
> No matter what I do I get the first nodes "<" encoded as "<" This
> means that the script that I am using can't recognise the first node.
> I would replace the characters but then the output is no longer
> recognised as XML.
>
> Why is this behaviour implemented? I basically end with this
>
> <Tables>
>     <XML_F52E2B61-18A1-11d1-B105-00805F49916B><row tID="1" tNo="11"
> tLeft="123" tTop="432"/></XML_F52E2B61-18A1-11d1-B105-00805F49916B>
> </Tables>
>
> Please assist urgent!
>
> Thanks
> sorCrer
>

AddThis Social Bookmark Button