Read XSD to string

S

sippyuconn

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
 
P

Pavel Minaev

sippyuconn said:
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.
 
S

Steven Cheng [MSFT]

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:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.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/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
 
S

Steven Cheng [MSFT]

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:
(e-mail address removed).

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

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

-----------------------------------------------------
From: (e-mail address removed) (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\
 

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

Top