473,387 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Read XSD to string

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
3 12433
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Xinyi Yang | last post by:
Hi, I have to read information out of a file. The format will be string1,string2,..., string3,string4,..., .... (the string sould not contain ' ' anyway) the size of each string is uncertain....
0
by: Ahmed A. | last post by:
This will be very helpfull for many! Using RichTextBox Read/Write Unicode File http://www.microsoft.com/indonesia/msdn/wnf_RichTextBox.as p Private Function ReadFile(ByVal myfile As String)...
4
by: Thomas Kreuzer | last post by:
Hello everyone, I have a question regarding how to interpret a string. Basically I want to write a little calculator, I will type something like "12+69*12-44/2" and then want my program to...
3
by: shaft | last post by:
hi i have the following code : string addrress; cin >> address; cout << address; if I enter any string with space in between , it will save the first word only. e.g. if i enter "this is...
5
by: lovecreatesbea... | last post by:
The condition at line 31 is added to check if the program finished to read the whole file. Is it needed and correct? Thank you. #include <fstream> #include <iostream> #include <string> using...
6
by: blinktude | last post by:
Hi I have a txt file full of words and numbers for example: ------------------------------------------------- `smash the stack` n. On many C implementations it is possible to corrupt the...
0
by: Yasin cepeci | last post by:
is it possible that read string values in a filled dataset?
1
by: boss1 | last post by:
i have textfile containing multiple rows.Each row containing same type of strings.now my problem is i can read only one row frequently but i can't read multiple rows. so how can i solve this...
1
by: Matrixinline | last post by:
Hi All, File Text.txt Contains following text as : "C:\program file\application data\details\app" "D:\Program File" I tried to read that data as fscanf(oFp, "%s %s", sCopyDirectory,...
3
by: maneshborase | last post by:
Hi friends, I am facing one serious problem in my application. I am trying to open dicom image file (.dcm) has size around 400 MB. But I am getting and unhandy exceptions, Some time, ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.