Click here to Skip to main content
15,993,754 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi,

I have xml data stored in a column of SQLserver table.
How do i access the data,its elements using c# code?


Thanks in advance..
Posted
Comments
Sergey Alexandrovich Kryukov 6-Aug-13 20:46pm    
What data type do you use to store XML?
—SA

1 solution

Normally, XML is stored in a Unicode string which is mapped via ADO.NET to .NET System.String. Then you can parse this string as XML. All XML parser can parse from any input stream. If you have a string representing XML content (not XML file, as in other cases), you can put the stream data in an instance of System.IO.MemoryStream:
http://msdn.microsoft.com/en-us/library/system.io.memorystream.aspx[^].

You can find a short code sample on how to work with the stream if you have a string: http://stackoverflow.com/questions/8143732/stringstream-in-c-sharp[^].

If, by some reason, you store XML as binary data (using BLOB data type), you still can use memory stream, which you initialize with raw data in the form of array of bytes, which you can obtain using ADO.NET DataReader. Please see this Microsoft article: http://msdn.microsoft.com/en-us/library/87z0hy49.aspx[^].

Now, you need to take one of XML parsers, initialize it from stream and parse it. What type of parser to choose? This is my short overview of them:
  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the class System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].


Good luck,
—SA
 
Share this answer
 
Comments
H.Brydon 7-Aug-13 0:38am    
Good answer, +5 to offset downvote.
Sergey Alexandrovich Kryukov 7-Aug-13 0:39am    
Thank you, Harvey.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900