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
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.


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

s = sw.ToString();

Open in new window

Ronak PatelCommented:
Lokesh B RDeveloperCommented:

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);
            return dataSet.Tables[0];

Open in new window

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:

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

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

From novice to tech pro — start learning today.