Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old September 6th, 2004, 10:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
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
Reply With Quote
  #2 (permalink)  
Old September 6th, 2004, 02:06 PM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
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.:)
Reply With Quote
  #3 (permalink)  
Old September 7th, 2004, 05:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
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
Reply With Quote
  #4 (permalink)  
Old September 7th, 2004, 11:40 AM
Friend of Wrox
 
Join Date: Jul 2004
Location: Tehran, , Iran.
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.:)
Reply With Quote
  #5 (permalink)  
Old September 8th, 2004, 04:53 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Cardiff, , United Kingdom.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 02:35 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.