SqlXml DataType and reader - how?

Posts   
 
    
helga
User
Posts: 17
Joined: 15-Jun-2007
# Posted on: 21-Jun-2007 13:32:57   

I usually wrote a function that retrieves the xml data from a reader and then i can further work with the returned information using the System.Data.SqlTypes namespace and the SqlXml method to read xml data from a table field that has xmldata in it as per the sample below. The readxml() and writexml() information in your documentation received very litte attention and i am quite confused as to how to use your generated code to achieve the same - and even furthermore - how to then create a dataset with datatables from specific nodes retrieved from this reader.

Here is a sample normal function

private void ReadSQLXmlWithSqlXml() { //Get the connectionstring string connString = GetConnectionString(); using (SqlConnection conn = new SqlConnection(connString)) { conn.Open();

                SqlCommand command = conn.CreateCommand();
                command.CommandText = "SELECT [xmlField] FROM [dbo].[TableName] WHERE ItemCode = 'CODE01'";
                SqlDataReader reader = command.ExecuteReader();

                System.Text.StringBuilder builder = new System.Text.StringBuilder();

                //Loop through all the nodes in the retried XML
                if (reader.Read())
                {
                    SqlXml sqlXmlValue = reader.GetSqlXml(0);
                    XmlReader xmlReader = sqlXmlValue.CreateReader();

                    //Loop through all the nodes in the retrieved xml
                    while (xmlReader.Read())
                    {
                        builder.Append(xmlReader.ReadString());
                        builder.Append("  ");
                    }
                    this.TextBox1.Text = builder.ToString();

                }
                else
                {
                    this.TextBox1.Text = "no value";
                }

            }

        }

Kind regards Helga

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 21-Jun-2007 15:21:15   

Which database are you using? And which version of LLBLGen Pro are you using?

If you are storing your data in a database column of XML type. LLBLGen Pro supports XML types for some databases. (please consult to the LLBLGen Pro manual: Concepts - Database drivers)

All you have to do is: Use the designer to map an entity to the specified table, and generate the code. When you fetch the entity, the XML database type will be mapped into a string .NET Type, and you may create a TypeConverter to convert this mapping to an XmlDocument for example. (please consult to the LLBLGen Pro manual: Concepts - Type Converters)

helga
User
Posts: 17
Joined: 15-Jun-2007
# Posted on: 21-Jun-2007 16:16:26   

Dear Walaa

Database: Microsoft SQL Server 2005 LLBLGenPro: 2.0.0.0.Final

OK, I will try your suggestion below, but is it not an idea to have something more quicker to use than to instantiate, get, instantiate, look, write use and bind as per the listing below. Maybe as future upgrade cause the xml sql datatype is extremely populer.

Looking at the description below, i'm inclined to think that it will be easier to bypass the llbl code and just directly query the database as per my sample? flushed

Regards Helga

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 22-Jun-2007 09:43:36   

The TypeConverter is only done once and you will have an XMLDocument field generated into your EntityClass and ready to be used.

If don't want to create a TypeConverter, then at least use LLBLGen to fetch the field as a string, then you can manually read it into any XML object. IMHO this will be easier than your solution (manually fetch the XML data).

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39863
Joined: 17-Aug-2003
# Posted on: 23-Jun-2007 11:13:55   

The SqlServer client treats XML fields as strings, so we return them as strings as well. As Walaa said, you can do two things: 1) create a type converter which converts strings to XmlDocument and back. This is fairly straight forward and will be transparent for you: the entity will have an XmlDocument typed field instead of a string typed field 2) you can also simply use the string and add that to an Xmldocument as its XML.

You then don't have to use the route of first converting the whole entity to XML, as that will likely give you more headaches than progress as the xml string you're looking for will be embedded in CDATA blocks

Frans Bouma | Lead developer LLBLGen Pro