Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 6th, 2004, 10:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default XML, Sockets, Streams and a headache

I am very new to VB.NET (I started learning it about 2 weeks ago) however I have several years of experience in VB5 and 6 so I am finding the transition problematic to say the least.

I am writing a class that I want to represent in XML. The XML Schema that I must conform to is fairly complicated so I have ruled out the option of using serialization. Therefore I want to use an XML stream and the XmlWriter or XmlTextWriter classes.

Fairly easy so far, however I need to be able to stream the XML via TCP/IP. Again this shouldn't be too much of a problem, but during testing I just want to display the XML in the console or in a TextBox on a form, or save the XML to a file.

My idea has been to have a function in my class called ToXML which returns a System.IO.Stream object and then I can do what I like with it the other end. But I do not know how to do this. If I create the XML using an instance of the System.Xml.XmlTextWriter then how do I just return the stream, then in the calling program, how do I display the stream, save the stream to a file, print it out to the console window or send it via TCP/IP?

Regards
Owain Williams
__________________
Regards
Owain Williams
 
Old September 6th, 2004, 02:06 PM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hello owain,
I cant get exactly what you mean
what do you mean from this sentence
Quote:
quote:
My idea has been to have a function in my class called ToXML which returns a System.IO.Stream object and then I can do what I like with it the other end
for working with your XML file you should make a stream for it
like
Code:
FileStream fs = new FileStream("Test.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite);
then you should make a XmlTextWriter object
Code:
System.Xml.XmlTextWriter xtw=new XmlTextWriter(fs,null);
as you see here we should give the constructor a stream object(FileStream)
then you can access this stream through Basestream property of your XmlTextWriter
Code:
 
//xtw.BaseStream
this baseStream is the same as the stream(or derived classes like FileStream) you declared in XmlTextWriter Constructor,
HtH.

--------------------------------------------
Mehdi.:)
 
Old September 7th, 2004, 05:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your reply. What I was trying to do was return an object of type System.IO.Stream that I could then use in the calling program, I now see the error of my way. What I am now doing (in VB.NET) is:
Code:
Public Class MyClass
    Public Sub ToXML(ByRef Stream As System.IO.Stream, _
                     ByRef Encoding As System.Text.Encoding)
        Dim xmlStream As System.Xml.XmlTextWriter
        xmlStream = New System.Xml.XmlTextWriter(Stream, Nothing)
        ' Build XML here...
    End Function

    Public Sub ToXML(ByRef Stream As System.IO.TextWriter)
        Dim xmlStream As System.Xml.XmlTextWriter
        xmlStream = New System.Xml.XmlTextWriter(Stream)
        ' Build XML here...
    End Function
End Class
I have overridden the ToXML subroutine to accept either a TextWriter or a Stream object and use this to build my XML into. This works in my calling program when I want to display the XML in the Console window by using code similar to this:
Code:
Private Sub BuildXml(ByVal sender As System.Object, _
                     ByVal e As System.EventArgs) _
                     Handles btnBuildXml.Click
    Dim xmlClass As MyClass = New MyClass
    xmlClass.ToXML(Console.Out)
End Sub
My next question is how can I change this to display the generated XML in a text box on a form? I have tried declaring an object of type System.IO.TextWriter and passing this to the ToXML subroutine, but how do I use this object when the subroutine completes to display the XML on a text box?

Another way of asking the question would be how do I use the System.Xml.XmlTextWriter to display the XML in a text box on a form?

Regards
Owain Williams
 
Old September 7th, 2004, 11:40 AM
Friend of Wrox
 
Join Date: Jul 2004
Posts: 623
Thanks: 0
Thanked 1 Time in 1 Post
Default

as I told you
xmlStreamWriter = New System.Xml.XmlTextWriter(Stream)
it writes to your stream ...(here your stream is Console.Out so you see them in screen)
but what does it write?something you passed them through xmlStreamWriter.WriteXXXXX methods
xmlStreamReader = New System.Xml.XmlTextWriter(Stream)
it reads from your stream now your stream should be your xml file(FileStream fs)and read from it and write it to your TextBox ....
(I'm not sure your second overridden method works correctly)
Quote:
quote:
Another way of asking the question would be how do I use the System.Xml.XmlTextWriter to display the XML in a text box on a form?
work with ur XML file like a simple TXT file and write it to ur textbox
(http://www.syncfusion.com/FAQ/WinForms/FAQ_c94c.asp)

--------------------------------------------
Mehdi.:)
 
Old September 8th, 2004, 04:53 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 231
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you for your time. The problem I have had with understanding this concept is that I know I want to use an object of type System.IO.Stream, however this class is declared as MustInherit so I have found it difficult to find the class I need to use. I am now using the System.IO.StringWriter class.

The class I am writing that generates the XML has not changed, however my calling program now does the following to display the XML in a text box on a form:
Code:
Dim xmlClass As MyClass = New MyClass
Dim myStream As System.IO.StringWriter

myStream = New System.IO.StringWriter
xmlClass.ToXML(myStream)
txtXML.Text = myStream.ToString()
Thanks again for your help.

Regards
Owain Williams





Similar Threads
Thread Thread Starter Forum Replies Last Post
SelectedValueChanged event is giving me a headache SQLScott Pro Visual Basic 2005 1 January 2nd, 2008 06:46 PM
Streams in C# pady123 C# 2 September 11th, 2007 10:17 AM
Sending XML through Sockets (ASP)? outlooksupport XML 1 June 27th, 2005 11:02 AM
iif Statement giving me a headache! MrMacca BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 January 7th, 2005 05:49 AM
Subform Headache GregoryHu Access VBA 0 July 9th, 2004 10:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.