473,382 Members | 1,736 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,382 software developers and data experts.

serialisation of classes to generate XMLDocument

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

Similar topics

1
by: Tim:. | last post by:
Hi Can someone tell me how I generate a new XML file that I can then use with an XSLT file. I am trying to use an XML file generated from Microsoft Project or even better generate an XML file from...
2
by: brad | last post by:
I have an XMLDocument that my web page downloads and manipulates as a response to user input. I have lots of JavaScript that acts as a controller between my view (html) and my model (xml). That...
5
by: Dario de Judicibus | last post by:
I am trying to generate an XML file to be shown in IE by javascript. My code looks like top.x = window.open('','MyXML') ; top.x.document.write('<?xml version="1.0" encoding="ISO-8859-1" ?>') ;...
4
by: Stephen | last post by:
Using the code below I am trying, in VB .Net 2003, to serialise classes defined in a couple of XSD documents. The encoding for both is Unicode(UTF-8). However the resulting XML is encoded as...
1
by: Ryan | last post by:
Can anyone point me to some examples of generating a complete XmlDocument given an XmlSchema object?
11
by: Crirus | last post by:
I need to save a quite big structure of classes... but only some members that I need to recondtruct the whole ierarchy. I want to ask how serialisation work... is a efficient way? Or should I deal...
6
by: paul perrin | last post by:
I have a class I want to implement in VB.Net - but can't get the behaviour I want. There are two issues - one how should I be getting the behaviour I want, and second is there a better way...
8
by: cd~ | last post by:
I can provide a test app, the news server won't allow me to post the files because they are too large (93KB and 1.2KB) I downloaded the ESRI ArcXml schema and generated the classes from the...
2
by: Greg | last post by:
I have a bizarre situation in which serialisation is failing routinely under a specific condition, and I'm wondering if the details ring a bell with anyone here. I have 2 classes that my...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: 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...

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.