C# convert Table to "XML local string s" without going through the MemoryStream step, and convert string s back to table

I want to convert my DataTable to an xml string (I will be saving the xml string in a db for later)

Later, I want to convert the string back into a DataTable.

Is there a direct way without going through the MemoryStream Class.  If not what is the best way?

Thanks Much
Sam
SamCashAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
Not really. You could use a StringWriter instead of the MemoryStream, though.

e.g.

DataTable dt = new DataTable();
StringWriter sw = new StringWriter();
string s;

dt.WriteXml(sw);
s = sw.ToString();

Open in new window

0
Ronak PatelCommented:
0
Lokesh B RDeveloperCommented:
Hi,

You can use the following methods.

Convert DataTable To XMLString

 public string ConvertDataTableToXMLString(DataTable dt)
 {
            System.IO.StringWriter writer = new System.IO.StringWriter();
            dt.WriteXml(writer, true);
            return writer.ToString();
 }

Open in new window


Convert XMLString To DataTable

       
 private DataTable ConvertXMLStringToDataTable(string xmlString)
        {
            DataSet dataSet = new DataSet();
            System.IO.StringReader stringReader = new System.IO.StringReader(xmlString);
            dataSet.ReadXml(stringReader);
            return dataSet.Tables[0];
        }

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SamCashAuthor Commented:
Guys,

All answers helped.  I can use Lokesh directly.  Thanks again.

Sam
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.