By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

Read XSD to string

P: n/a
Hi

I have an xsd that I want to save as an XML string to store in a DB
I can save as a physical file using
xsd.WriteXml(@"C:\Temp\Junk\junk.xml");
But I am unable to save to a string so I can write string to a db
I tried to a MemoryStream but it seems to be empty ???
There is data
because the above WriteXML files a text file

MemoryStream ms = new MemoryStream();
xsd.WriteXml(ms);
// Create a stream reader.
StreamReader reader = new StreamReader(ms);

// Just read to the end.
string sXMLFile = reader.ReadToEnd();

Thanks

Jul 2 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
sippyuconn wrote:
Hi

I have an xsd that I want to save as an XML string to store in a DB
I can save as a physical file using
xsd.WriteXml(@"C:\Temp\Junk\junk.xml");
But I am unable to save to a string so I can write string to a db
I tried to a MemoryStream but it seems to be empty ???
There is data
because the above WriteXML files a text file

MemoryStream ms = new MemoryStream();
xsd.WriteXml(ms);
// Create a stream reader.
StreamReader reader = new StreamReader(ms);

// Just read to the end.
string sXMLFile = reader.ReadToEnd();
The stream is not empty; what you forget is that, after data is
written to the stream, its current position is at the end. You need to
manually set it to the start of the stream to read what you've just
wrote, like this:

ms.Position = 0

Also, if what you want is to get a plain C# string, then you might
want to use StringWriter instead of MemoryStream.
Jul 2 '08 #2

P: n/a
Hi sippy,

In addition to Pavel's suggestion. here is another way you can try:

Using the StringWriter which can help get the XmlSchema(or any other class
which will need a stream/TextWriter to output). And you can use
StringWriter.ToString() to get the underlying outputed string. Here is a
simple test code you can refer to:

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

static void Main(string[] args)
{
//load xsd from file
StreamReader sr = new StreamReader(@"..\..\test.xsd");
XmlSchema xsd = XmlSchema.Read(sr, null);
sr.Close();

//output it into string
StringWriter sw = new StringWriter();
xsd.Write(sw);

string data2store = sw.ToString();
//import it from string
XmlSchema xsd1 = XmlSchema.Read(new StringReader(data2store),
null);

xsd1.Write(Console.Out);
}
===========================

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------
>Thread-Topic: Read XSD to string
thread-index: AcjcYZLSCJ8yvWwtRpmcc50g7H1zhQ==
X-WBNR-Posting-Host: 65.55.21.8
From: =?Utf-8?B?c2lwcHl1Y29ubg==?= <si********@newsgroup.nospam>
Subject: Read XSD to string
Date: Wed, 2 Jul 2008 09:35:01 -0700
>
Hi

I have an xsd that I want to save as an XML string to store in a DB
I can save as a physical file using
xsd.WriteXml(@"C:\Temp\Junk\junk.xml");
But I am unable to save to a string so I can write string to a db
I tried to a MemoryStream but it seems to be empty ???
There is data
because the above WriteXML files a text file

MemoryStream ms = new MemoryStream();
xsd.WriteXml(ms);
// Create a stream reader.
StreamReader reader = new StreamReader(ms);

// Just read to the end.
string sXMLFile = reader.ReadToEnd();

Thanks

Jul 3 '08 #3

P: n/a
Hi sippy,

How are you doing?

Does the suggestion in my last reply help you on this issue? If there is
any further questions on this, welcome to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

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

-----------------------------------------------------
>From: st*****@online.microsoft.com (Steven Cheng [MSFT])
Organization: Microsoft
Date: Thu, 03 Jul 2008 02:24:52 GMT
Subject: RE: Read XSD to string
>Hi sippy,

In addition to Pavel's suggestion. here is another way you can try:

Using the StringWriter which can help get the XmlSchema(or any other class
which will need a stream/TextWriter to output). And you can use
StringWriter.ToString() to get the underlying outputed string. Here is a
simple test code you can refer to:

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

static void Main(string[] args)
{
//load xsd from file
StreamReader sr = new StreamReader(@"..\..\test.xsd");
XmlSchema xsd = XmlSchema.Read(sr, null);
sr.Close();

//output it into string
StringWriter sw = new StringWriter();
xsd.Write(sw);

string data2store = sw.ToString();
//import it from string
XmlSchema xsd1 = XmlSchema.Read(new StringReader(data2store),
null);

xsd1.Write(Console.Out);
}
===========================

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
D\
Jul 7 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.