By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,656 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,656 IT Pros & Developers. It's quick & easy.

serialisation of classes to generate XMLDocument

P: n/a
I need to generate input XML for another application by serialising classes
defined in an XSD document. The code below will generate the XML I require
but I need to generate this in memory rather than creating a file. I assume
I should be using System.IO.MemoryStream but can't get this to work.

' XML will be serialized to file.xml, in UTF-8, with BOM.
Dim tw As New System.Xml.XmlTextWriter( _
New System.IO.FileStream("file.xml",
System.IO.FileMode.Create), _
New System.Text.UTF8Encoding(True))
ser.Serialize(tw, domainObj.getGMPApp)
' Finish writing the file and close it.
tw.Flush()
tw.Close()
xmlDoc.Load("file.xml")
Nov 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
how about serializing to a string?

<!--StartFragment-->
XmlSerializer s1 = new XmlSerializer(typeof(MyType));
System.IO.StringWriter sw = new System.IO.StringWriter();
s1.Serialize(sw, instance);
// sw.ToString() now holds the XML ...
"Stephen" <St*****@discussions.microsoft.com> wrote in message
news:9C**********************************@microsof t.com...
I need to generate input XML for another application by serialising classes
defined in an XSD document. The code below will generate the XML I
require
but I need to generate this in memory rather than creating a file. I
assume
I should be using System.IO.MemoryStream but can't get this to work.

' XML will be serialized to file.xml, in UTF-8, with BOM.
Dim tw As New System.Xml.XmlTextWriter( _
New System.IO.FileStream("file.xml",
System.IO.FileMode.Create), _
New System.Text.UTF8Encoding(True))
ser.Serialize(tw, domainObj.getGMPApp)
' Finish writing the file and close it.
tw.Flush()
tw.Close()
xmlDoc.Load("file.xml")

Nov 12 '05 #2

P: n/a
dino
this generates XML with encoding iof UTF-16 whch I can't load into the XML
documentthat I want to. The code that generates the file will generate the
XML with the correct encoding but what I want too be able to do is generate
this in code.
"Dino Chiesa [Microsoft]" wrote:
how about serializing to a string?

<!--StartFragment-->
XmlSerializer s1 = new XmlSerializer(typeof(MyType));
System.IO.StringWriter sw = new System.IO.StringWriter();
s1.Serialize(sw, instance);
// sw.ToString() now holds the XML ...
"Stephen" <St*****@discussions.microsoft.com> wrote in message
news:9C**********************************@microsof t.com...
I need to generate input XML for another application by serialising classes
defined in an XSD document. The code below will generate the XML I
require
but I need to generate this in memory rather than creating a file. I
assume
I should be using System.IO.MemoryStream but can't get this to work.

' XML will be serialized to file.xml, in UTF-8, with BOM.
Dim tw As New System.Xml.XmlTextWriter( _
New System.IO.FileStream("file.xml",
System.IO.FileMode.Create), _
New System.Text.UTF8Encoding(True))
ser.Serialize(tw, domainObj.getGMPApp)
' Finish writing the file and close it.
tw.Flush()
tw.Close()
xmlDoc.Load("file.xml")


Nov 12 '05 #3

P: n/a
my bad...

Dim ms As New System.IO.MemoryStream()
Dim tw2 As New System.Xml.XmlTextWriter( ms, utf8 )
Try
ser.Serialize(tw2, dto1)
tw2.Flush()
ms.Seek(0, System.IO.SeekOrigin.Begin)

Dim byteArray as Byte()
byteArray= New Byte(CType(ms.Length, Integer)){}
Dim count As Integer
count = 0

' Read the Bytes, Byte by Byte.
While(count < ms.Length)
byteArray(count) = _
System.Convert.ToByte(ms.ReadByte())
count += 1
End While

' Decode the Byte array into a Char array
' and write it to the console.
Dim charArray as Char()
charArray = _
New Char(utf8.GetCharCount(byteArray, 0, count)){}
utf8.GetDecoder().GetChars( byteArray, 0, count, charArray, 0)
System.Console.WriteLine(charArray)

Finally
ms.Close()
tw2.Close()
End Try


"Stephen" <St*****@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.com...
dino
this generates XML with encoding iof UTF-16 whch I can't load into the XML
documentthat I want to. The code that generates the file will generate
the
XML with the correct encoding but what I want too be able to do is
generate
this in code.
"Dino Chiesa [Microsoft]" wrote:
how about serializing to a string?

<!--StartFragment-->
XmlSerializer s1 = new XmlSerializer(typeof(MyType));
System.IO.StringWriter sw = new System.IO.StringWriter();
s1.Serialize(sw, instance);
// sw.ToString() now holds the XML ...
"Stephen" <St*****@discussions.microsoft.com> wrote in message
news:9C**********************************@microsof t.com...
>I need to generate input XML for another application by serialising
>classes
> defined in an XSD document. The code below will generate the XML I
> require
> but I need to generate this in memory rather than creating a file. I
> assume
> I should be using System.IO.MemoryStream but can't get this to work.
>
> ' XML will be serialized to file.xml, in UTF-8, with BOM.
> Dim tw As New System.Xml.XmlTextWriter( _
> New System.IO.FileStream("file.xml",
> System.IO.FileMode.Create), _
> New System.Text.UTF8Encoding(True))
> ser.Serialize(tw, domainObj.getGMPApp)
> ' Finish writing the file and close it.
> tw.Flush()
> tw.Close()
> xmlDoc.Load("file.xml")
>
>


Nov 12 '05 #4

P: n/a
Dino
thank U for the code below, which when I run it generates the required XML
in the charArray. However I am still having problems using this XML. What I
want to do is take this XML and use it in an XSL transformation to generate
the input XML for another application. Do you know what I need to do to load
the serialised XML into something that the transform can use?
..
..
Dim xslt As New Xml.Xsl.XslTransform
Dim xr As Xml.XmlResolver
Dim transformXSL As [String] = "transform.xsl"
xslt.Load(transformXSL)
writer.Formatting = Xml.Formatting.Indented
xslt.Transform(---- XML ----, Nothing, ---Output XM L---, Nothing)

"Dino Chiesa [Microsoft]" wrote:
my bad...

Dim ms As New System.IO.MemoryStream()
Dim tw2 As New System.Xml.XmlTextWriter( ms, utf8 )
Try
ser.Serialize(tw2, dto1)
tw2.Flush()
ms.Seek(0, System.IO.SeekOrigin.Begin)

Dim byteArray as Byte()
byteArray= New Byte(CType(ms.Length, Integer)){}
Dim count As Integer
count = 0

' Read the Bytes, Byte by Byte.
While(count < ms.Length)
byteArray(count) = _
System.Convert.ToByte(ms.ReadByte())
count += 1
End While

' Decode the Byte array into a Char array
' and write it to the console.
Dim charArray as Char()
charArray = _
New Char(utf8.GetCharCount(byteArray, 0, count)){}
utf8.GetDecoder().GetChars( byteArray, 0, count, charArray, 0)
System.Console.WriteLine(charArray)

Finally
ms.Close()
tw2.Close()
End Try


"Stephen" <St*****@discussions.microsoft.com> wrote in message
news:7B**********************************@microsof t.com...
dino
this generates XML with encoding iof UTF-16 whch I can't load into the XML
documentthat I want to. The code that generates the file will generate
the
XML with the correct encoding but what I want too be able to do is
generate
this in code.
"Dino Chiesa [Microsoft]" wrote:
how about serializing to a string?

<!--StartFragment-->
XmlSerializer s1 = new XmlSerializer(typeof(MyType));
System.IO.StringWriter sw = new System.IO.StringWriter();
s1.Serialize(sw, instance);
// sw.ToString() now holds the XML ...
"Stephen" <St*****@discussions.microsoft.com> wrote in message
news:9C**********************************@microsof t.com...
>I need to generate input XML for another application by serialising
>classes
> defined in an XSD document. The code below will generate the XML I
> require
> but I need to generate this in memory rather than creating a file. I
> assume
> I should be using System.IO.MemoryStream but can't get this to work.
>
> ' XML will be serialized to file.xml, in UTF-8, with BOM.
> Dim tw As New System.Xml.XmlTextWriter( _
> New System.IO.FileStream("file.xml",
> System.IO.FileMode.Create), _
> New System.Text.UTF8Encoding(True))
> ser.Serialize(tw, domainObj.getGMPApp)
> ' Finish writing the file and close it.
> tw.Flush()
> tw.Close()
> xmlDoc.Load("file.xml")
>
>


Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.