473,772 Members | 2,510 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Memor yStream 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.XmlT extWriter( _
New System.IO.FileS tream("file.xml ",
System.IO.FileM ode.Create), _
New System.Text.UTF 8Encoding(True) )
ser.Serialize(t w, domainObj.getGM PApp)
' Finish writing the file and close it.
tw.Flush()
tw.Close()
xmlDoc.Load("fi le.xml")
Nov 12 '05 #1
4 2078
how about serializing to a string?

<!--StartFragment-->
XmlSerializer s1 = new XmlSerializer(t ypeof(MyType));
System.IO.Strin gWriter sw = new System.IO.Strin gWriter();
s1.Serialize(sw , instance);
// sw.ToString() now holds the XML ...
"Stephen" <St*****@discus sions.microsoft .com> wrote in message
news:9C******** *************** ***********@mic rosoft.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.Memor yStream 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.XmlT extWriter( _
New System.IO.FileS tream("file.xml ",
System.IO.FileM ode.Create), _
New System.Text.UTF 8Encoding(True) )
ser.Serialize(t w, domainObj.getGM PApp)
' Finish writing the file and close it.
tw.Flush()
tw.Close()
xmlDoc.Load("fi le.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(t ypeof(MyType));
System.IO.Strin gWriter sw = new System.IO.Strin gWriter();
s1.Serialize(sw , instance);
// sw.ToString() now holds the XML ...
"Stephen" <St*****@discus sions.microsoft .com> wrote in message
news:9C******** *************** ***********@mic rosoft.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.Memor yStream 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.XmlT extWriter( _
New System.IO.FileS tream("file.xml ",
System.IO.FileM ode.Create), _
New System.Text.UTF 8Encoding(True) )
ser.Serialize(t w, domainObj.getGM PApp)
' Finish writing the file and close it.
tw.Flush()
tw.Close()
xmlDoc.Load("fi le.xml")


Nov 12 '05 #3
my bad...

Dim ms As New System.IO.Memor yStream()
Dim tw2 As New System.Xml.XmlT extWriter( ms, utf8 )
Try
ser.Serialize(t w2, dto1)
tw2.Flush()
ms.Seek(0, System.IO.SeekO rigin.Begin)

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

' Read the Bytes, Byte by Byte.
While(count < ms.Length)
byteArray(count ) = _
System.Convert. ToByte(ms.ReadB yte())
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.GetCh arCount(byteArr ay, 0, count)){}
utf8.GetDecoder ().GetChars( byteArray, 0, count, charArray, 0)
System.Console. WriteLine(charA rray)

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


"Stephen" <St*****@discus sions.microsoft .com> wrote in message
news:7B******** *************** ***********@mic rosoft.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(t ypeof(MyType));
System.IO.Strin gWriter sw = new System.IO.Strin gWriter();
s1.Serialize(sw , instance);
// sw.ToString() now holds the XML ...
"Stephen" <St*****@discus sions.microsoft .com> wrote in message
news:9C******** *************** ***********@mic rosoft.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.Memor yStream 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.XmlT extWriter( _
> New System.IO.FileS tream("file.xml ",
> System.IO.FileM ode.Create), _
> New System.Text.UTF 8Encoding(True) )
> ser.Serialize(t w, domainObj.getGM PApp)
> ' Finish writing the file and close it.
> tw.Flush()
> tw.Close()
> xmlDoc.Load("fi le.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.XslTran sform
Dim xr As Xml.XmlResolver
Dim transformXSL As [String] = "transform. xsl"
xslt.Load(trans formXSL)
writer.Formatti ng = Xml.Formatting. Indented
xslt.Transform(---- XML ----, Nothing, ---Output XM L---, Nothing)

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

Dim ms As New System.IO.Memor yStream()
Dim tw2 As New System.Xml.XmlT extWriter( ms, utf8 )
Try
ser.Serialize(t w2, dto1)
tw2.Flush()
ms.Seek(0, System.IO.SeekO rigin.Begin)

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

' Read the Bytes, Byte by Byte.
While(count < ms.Length)
byteArray(count ) = _
System.Convert. ToByte(ms.ReadB yte())
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.GetCh arCount(byteArr ay, 0, count)){}
utf8.GetDecoder ().GetChars( byteArray, 0, count, charArray, 0)
System.Console. WriteLine(charA rray)

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


"Stephen" <St*****@discus sions.microsoft .com> wrote in message
news:7B******** *************** ***********@mic rosoft.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(t ypeof(MyType));
System.IO.Strin gWriter sw = new System.IO.Strin gWriter();
s1.Serialize(sw , instance);
// sw.ToString() now holds the XML ...
"Stephen" <St*****@discus sions.microsoft .com> wrote in message
news:9C******** *************** ***********@mic rosoft.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.Memor yStream 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.XmlT extWriter( _
> New System.IO.FileS tream("file.xml ",
> System.IO.FileM ode.Create), _
> New System.Text.UTF 8Encoding(True) )
> ser.Serialize(t w, domainObj.getGM PApp)
> ' Finish writing the file and close it.
> tw.Flush()
> tw.Close()
> xmlDoc.Load("fi le.xml")
>
>


Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2343
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 the project file (MPP File) itself. Which I can then use with an XSLT file I am new to XML and want to create a project viewer using XML, ASP and XHTML. I have an idea of how this should work but would really appritiate any help someone can...
2
5066
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 works fine. Once the user's done editing, I want to send the modified XMLDocument back up to my server (as XML). The problem is, I can't find a way to use JavaScript to generate XML from an XMLDocument. Is there some obvious API that I'm missing?...
5
2004
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" ?>') ; top.x.document.write('<myxml>') ; // all the other write's top.x.document.write('</myxml>') ;
4
5736
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 UTF-16. This is causing me problems when I try to load it into an XPath document. I would imagine I should be able to use System.Text.Encoding to define the encoding as UTF-8 but I haven't been able to figure out how so far. Dim ser As New...
1
1737
by: Ryan | last post by:
Can anyone point me to some examples of generating a complete XmlDocument given an XmlSchema object?
11
1297
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 myself with persistent members in my format? Thanks, Crirus --
6
1434
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 altogether? 1) How do I... I have two classes, one represents a 'data record', the other is a generic 'data field'.
8
5466
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 schema using xsd.exe, which took a while because xsd doesn't handle recursive elements very well (StackOverFlow exception during generation). Now that I have the classes I am trying to serialize them to an xml document to send to ArcIMS to generate...
2
1337
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 application serialises before closing down. One of these classes contains a string field which contains a path to a file. This is populated from the contents of a text box, which in turn is populated using an OpenFileFialog object. Under normal usage,...
0
9621
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10264
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7461
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2851
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.