XML question

  • Thread starter Thread starter Stan SR
  • Start date Start date
S

Stan SR

Hi,

I m a newbie with c#.

Here's what I need to do :

Create in memory a xml doc

<mydata>
<uid>blabla</uid>
<name>Stan</name>
<age>22</age>
</mydata>


Save the xml doc inside a table
(for this part, I just need to know how to convert the xml doc to (I
suppose) a string variable).


Read the xml from the table.
I think that I have to convert the string field into a xml doc

and manipulate the xml doc in memory.

If you could help on some of this points.

regards

Stan
 
Stan,
Use the System.Xml.XmlDocument object.
Check out the LoadXml method and InnerXML or InnerText properties.
 
Thanks
I understand the loadXml method
but I don't know how to convert (or store) the xml document into a table (I
use SQL 2000 server).
My questions are :
which kind of field type I have to create in my table (string, binary, etc
?)
and how to save the xml doc in this field.
I suppose I have to convert the xml doc into string or anything else ?

Any idea ?

Stan
 
Hi Stan,

If you are looking to get your XML data into a System.Data.DataSet
object, there is a function/method of the DataSet object that would be
very useful to you: ReadXML().

You could use it somthing along the lines of (in VB):

Dim ds as New System.Data.DataSet()

ds.ReadXML(your_xml_doc.xml)

The 'your_xml_doc.xml' doesn't have to be a file - it could be a
string, IO.Stream, a IO.TextReader, or XML.XmlReader...

Hope this helps!
Greg
 
There are several options.. you need to be a little clearer in your needs.
(I'm not saying, not attacking you).

Do you want to stored your data in the database as
1. Tables and Relationships ... ?
2. Just xml documents?

If you're trying to get the data OUT of sql server as xml and you're using
#1. then search BOL (Books OnLine) for "FOR XML" (or one variation of that
"FOR XML AUTO")

If you're just storing a big hunk of text in your database, then use the
previous post which talks about streams.

If you want to put xml back into the database as
#1 ... then you need to become familar with the OPENXML call in tsql
see
http://www.sqlservercentral.com/col...eragingxpexcelxmlandopenxmlfordataimports.asp
for an instance of that.


if you're stroing your xml in the database as just a big text string.. then
writing a stored procedure like

uspInsertBigXML ( uid int , myxml text)

is what you're looking for ..........
 
"sloan" a écrit dans le message de (e-mail address removed)...
There are several options.. you need to be a little clearer in your needs.
(I'm not saying, not attacking you).
:-)


Do you want to stored your data in the database as
1. Tables and Relationships ... ?
2. Just xml documents?

Just XML documents
So that means create in memory a XML document, store it in a database field
And have the possibility to load it from the field like a xml document that
I can parse use the xml functions.
Thanks

Stan
 
Try Text or nText for you table column type, then you want have to worry
about your XML size growing larger than what a varchar can take.

Also you can use the T-SQL OpenXml command to perform XML queries on the SQL
server.

Garth.
 
Thanks

I ve found this bit of code that converts a xml document to a string.
So I think I finally find the issue to my problem with your help.

Stan

static string GetXmlString(string strFile)
{
XmlDocument xmlDoc = new XmlDocument();
try
{
xmlDoc.LoadXml(strFile);
}
catch (XmlException e)
{
Console.WriteLine(e.Message);
}
StringWriter sw = new StringWriter();
XmlTextWriter xw = new XmlTextWriter(sw);
xmlDoc.WriteTo(xw);
return sw.ToString();
}
 

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

Back
Top