reading xml from database..

P

prashant

Hi all,
i am working with .net 2.0 and sql server managment studio of
version--9.00.1399.00

for getting a xmldocument from a xml data in sqlserver ..

their is no error in this program but xmlreader returned by
line-->
"XmlReader xmlUser = xmlMain.CreateReader();

shows that xmlUser.HasValue is false but the "xmlMain" has the Xml
document...and the end result i am getting is null in XmlDocumnet named
userdoc....and thats not our requirment

the function is following:--

public static XmlDocument GetXmlDocument(string coloumName,
SqlDataReader sqlReader)
{
int ordinal = sqlReader.GetOrdinal(coloumName);
XmlDocument userDoc = new XmlDocument();
if (ordinal > 0)
{
SqlXml xmlMain = sqlReader.GetSqlXml(ordinal);

if (!xmlMain.IsNull)
{
XmlReader xmlUser = xmlMain.CreateReader();
if (xmlUser.HasValue)
{
userDoc.Load(xmlUser);
}
}
return userDoc;


Thanx in advance........
 
G

Guest

Hi all,
i am working with .net 2.0 and sql server managment studio of
version--9.00.1399.00

for getting a xmldocument from a xml data in sqlserver ..

their is no error in this program but xmlreader returned by
line-->
"XmlReader xmlUser = xmlMain.CreateReader();

shows that xmlUser.HasValue is false but the "xmlMain" has the Xml
document...and the end result i am getting is null in XmlDocumnet named
userdoc....and thats not our requirment

the function is following:--

public static XmlDocument GetXmlDocument(string coloumName,
SqlDataReader sqlReader)
{
int ordinal = sqlReader.GetOrdinal(coloumName);
XmlDocument userDoc = new XmlDocument();
if (ordinal > 0)
{
SqlXml xmlMain = sqlReader.GetSqlXml(ordinal);

if (!xmlMain.IsNull)
{
XmlReader xmlUser = xmlMain.CreateReader();
if (xmlUser.HasValue)
{
userDoc.Load(xmlUser);
}
}
return userDoc;

Thanx in advance........

HasValue returns a true if the node on which the reader is currently
positioned has a value. You suppose you wanted to read the first node

XmlReader xmlUser = xmlMain.CreateReader();
if (xmlUser.Read)
{
userDoc.Load(xmlUser);
}
 
G

Guest

HasValue returns a true if the node on which the reader is currently
positioned has a value. You suppose you wanted to read the first node

XmlReader xmlUser = xmlMain.CreateReader();
if (xmlUser.Read)
{
userDoc.Load(xmlUser);

must be if (xmlUser.Read())
 
S

Steven Cheng[MSFT]

Hi

As Alexey has suggested, after you create the XmlReader from SqlXml
instance, you can call its "Read()" method to read into the underlying XML
stream. Also, you need to first call "Read" method on the SqlDataReader you
get so as to move to the first returned record:

e.g.

================
using (SqlCommand command =
connection.CreateCommand())
{
command.CommandType = CommandType.Text;
command.CommandText = sql;
command.Parameters.AddRange(sqlParams);

connection.Open();

using (SqlDataReader dr =
command.ExecuteReader())
{
if (dr.Read())
document.Load(
dr.GetSqlXml(0).CreateReader());
}

connection.Close();
}

===========================

Here are some web articles discussing on this:

#XML Data Type Support in ADO.NET 2.0: Handling XML from SQL Server 2005
http://msdn2.microsoft.com/en-us/library/ms971534.aspx

#XML Data type in ADO.Net v2.0 - Part II
http://blogs.msdn.com/sushilc/rss_tag_SQL+Server_2700_s+Xml+DataType+w_2F00_
ADO.Net+2.0.xml

#Work with XML Data Type in SQL Server 2005 from ADO.NET 2.0
http://www.developer.com/net/net/article.php/11087_3406251_3

Hope this helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
S

Steven Cheng[MSFT]

Hi prashant,

How are you doing on this issue, have you got any progress? If there is
still anything we can help, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top