Read XSD to string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?c2lwcHl1Y29ubg==?=

    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\ju nk.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.ReadToEn d();



    Thanks

  • Pavel Minaev

    #2
    Re: Read XSD to string

    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\ju nk.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.ReadToEn d();
    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.

    Comment

    • Steven Cheng [MSFT]

      #3
      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.To String() 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(da ta2store),
      null);

      xsd1.Write(Cons ole.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:
      msdnmg@microsof t.com.

      =============== =============== =============== =====
      Get notification to my posts through email? Please refer to
      Gain technical skills through documentation and training, earn certifications and connect with the community

      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: AcjcYZLSCJ8yvWw tRpmcc50g7H1zhQ ==
      >X-WBNR-Posting-Host: 65.55.21.8
      >From: =?Utf-8?B?c2lwcHl1Y29 ubg==?= <sippyuconn@new sgroup.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\j unk.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(m s);
      >// Create a stream reader.
      >StreamReader reader = new StreamReader(ms );
      >
      >// Just read to the end.
      >string sXMLFile = reader.ReadToEn d();
      >
      >
      >
      >Thanks
      >
      >

      Comment

      • Steven Cheng [MSFT]

        #4
        RE: Read XSD to string

        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:
        msdnmg@microsof t.com.

        =============== =============== =============== =====
        Get notification to my posts through email? Please refer to
        Gain technical skills through documentation and training, earn certifications and connect with the community

        ications.

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

        -----------------------------------------------------
        >From: stcheng@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.T oString() 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(da ta2store),
        >null);
        >
        xsd1.Write(Cons ole.Out);
        }
        >============== =============
        >
        >Sincerely,
        >
        >Steven Cheng
        >
        >Microsoft MSDN Online Support Lead
        >
        >
        >D\

        Comment

        Working...