473,405 Members | 2,294 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,405 software developers and data experts.

Serialize object to XML with nested structure

I would like to serialize a class to XML. The format of the XML is
fixed (see below). It only needs to be serialized - it is read by
another application.

I know I can do it manually using XMLTextWriter - e.g.

Dim xwriter = New
XmlTextWriter("C:\Projects\Formflo\WindowsService\ NDde\test.xml",
System.Text.Encoding.UTF8)
xwriter.Formatting = Formatting.Indented
xwriter.Indentation = 4

xwriter.WriteStartDocument(True)

'Write an element (this one is the root).
xwriter.WriteStartElement("PRODTICKER")
xwriter.WriteStartElement("COUNTERDISPLAY")
xwriter.WriteStartElement("COUNTERITEM")
However, I would like to make it a bit more "elegant" and create a
Serializable class. The actual values in the document
("CURRENTSHIFTDISPLAY", "3471") are coming from a DDE feed (please
don't ask!)

I guess what I'm asking (and I've looked for the answers but can't find
them is:

How do you serialize this to retain the "nestedness" - i.e. the
structure?

How do you serialize repeating lists?

Thanks

Edward

=============REQUIRED XML====================

<?xml version="1.0" encoding="utf-8" ?>
<PRODTICKER>
<COUNTERDISPLAY>
<COUNTERITEM>
<CTITLE>CURRENTSHIFTDISPLAY</CTITLE>
<CVALUE>3471</CVALUE>
</COUNTERITEM>
<COUNTERITEM>
<CTITLE>CURRENTSHIFTTARGETDISPLAY</CTITLE>
<CVALUE>4377</CVALUE>
</COUNTERITEM>
<COUNTERITEM>
<CTITLE>LASTSHIFTDISPLAY</CTITLE>
<CVALUE>8460</CVALUE>
</COUNTERITEM>
<COUNTERITEM>
<CTITLE>SECONDLASTHIFTDISPLAY</CTITLE>
<CVALUE>7146</CVALUE>
</COUNTERITEM>
<COUNTERITEM>
<CTITLE>THIRDLASTSHIFTDISPLAY</CTITLE>
<CVALUE>7354</CVALUE>
</COUNTERITEM>
</COUNTERDISPLAY>
<STATUSLIGHTS>
<STATUSLIGHTITEM>
<LINENUMBER>6</LINENUMBER>
<STATUS>1</STATUS>
<LINEVALUE>366</LINEVALUE>
</STATUSLIGHTITEM>
<STATUSLIGHTITEM>
<LINENUMBER>7</LINENUMBER>
<STATUS>1</STATUS>
<LINEVALUE>2</LINEVALUE>
</STATUSLIGHTITEM>
<STATUSLIGHTITEM>
<LINENUMBER>8</LINENUMBER>
<STATUS>4</STATUS>
<LINEVALUE>467</LINEVALUE>
</STATUSLIGHTITEM>
<STATUSLIGHTITEM>
<LINENUMBER>9</LINENUMBER>
<STATUS>2</STATUS>
<LINEVALUE>790</LINEVALUE>
</STATUSLIGHTITEM>
<STATUSLIGHTITEM>
<LINENUMBER>10</LINENUMBER>
<STATUS>8</STATUS>
<LINEVALUE>427</LINEVALUE>
</STATUSLIGHTITEM>
</STATUSLIGHTS>
</PRODTICKER>

Dec 4 '06 #1
4 2234
Those look like ordinary linear (i.e. not nested) property values to me. So
what you could do is write a node:

theXMLSubNode = m_XML.CreateElement("mySubNode")
theXMLDocument.AppendChild(theXMLSubNodes )

and then set these as attributes:

theXMLAttribute1 = m_XML.CreateAttribute("PRODTICKER")
theXMLAttribute1.Value = "3471"
theXMLSubNode .Attributes.Append(theXMLAttribute1)

theXMLAttribute2 = m_XML.CreateAttribute("COUNTERDISPLAY")
theXMLAttribute2.Value = "1234"
theXMLSubNode .Attributes.Append(theXMLAttribute2)
etc. The attributes will be "inside" the sub node. If you want to nest
further, create new sub-nodes and append them to their "parent" nodes.


Robin

<te********@hotmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
>I would like to serialize a class to XML. The format of the XML is
fixed (see below). It only needs to be serialized - it is read by
another application.

I know I can do it manually using XMLTextWriter - e.g.

Dim xwriter = New
XmlTextWriter("C:\Projects\Formflo\WindowsService\ NDde\test.xml",
System.Text.Encoding.UTF8)
xwriter.Formatting = Formatting.Indented
xwriter.Indentation = 4

xwriter.WriteStartDocument(True)

'Write an element (this one is the root).
xwriter.WriteStartElement("PRODTICKER")
xwriter.WriteStartElement("COUNTERDISPLAY")
xwriter.WriteStartElement("COUNTERITEM")
However, I would like to make it a bit more "elegant" and create a
Serializable class. The actual values in the document
("CURRENTSHIFTDISPLAY", "3471") are coming from a DDE feed (please
don't ask!)

I guess what I'm asking (and I've looked for the answers but can't find
them is:

How do you serialize this to retain the "nestedness" - i.e. the
structure?

How do you serialize repeating lists?

Thanks

Edward

=============REQUIRED XML====================

<?xml version="1.0" encoding="utf-8" ?>
<PRODTICKER>
<COUNTERDISPLAY>
<COUNTERITEM>
<CTITLE>CURRENTSHIFTDISPLAY</CTITLE>
<CVALUE>3471</CVALUE>
</COUNTERITEM>
<COUNTERITEM>
<CTITLE>CURRENTSHIFTTARGETDISPLAY</CTITLE>
<CVALUE>4377</CVALUE>
</COUNTERITEM>
<COUNTERITEM>
<CTITLE>LASTSHIFTDISPLAY</CTITLE>
<CVALUE>8460</CVALUE>
</COUNTERITEM>
<COUNTERITEM>
<CTITLE>SECONDLASTHIFTDISPLAY</CTITLE>
<CVALUE>7146</CVALUE>
</COUNTERITEM>
<COUNTERITEM>
<CTITLE>THIRDLASTSHIFTDISPLAY</CTITLE>
<CVALUE>7354</CVALUE>
</COUNTERITEM>
</COUNTERDISPLAY>
<STATUSLIGHTS>
<STATUSLIGHTITEM>
<LINENUMBER>6</LINENUMBER>
<STATUS>1</STATUS>
<LINEVALUE>366</LINEVALUE>
</STATUSLIGHTITEM>
<STATUSLIGHTITEM>
<LINENUMBER>7</LINENUMBER>
<STATUS>1</STATUS>
<LINEVALUE>2</LINEVALUE>
</STATUSLIGHTITEM>
<STATUSLIGHTITEM>
<LINENUMBER>8</LINENUMBER>
<STATUS>4</STATUS>
<LINEVALUE>467</LINEVALUE>
</STATUSLIGHTITEM>
<STATUSLIGHTITEM>
<LINENUMBER>9</LINENUMBER>
<STATUS>2</STATUS>
<LINEVALUE>790</LINEVALUE>
</STATUSLIGHTITEM>
<STATUSLIGHTITEM>
<LINENUMBER>10</LINENUMBER>
<STATUS>8</STATUS>
<LINEVALUE>427</LINEVALUE>
</STATUSLIGHTITEM>
</STATUSLIGHTS>
</PRODTICKER>

Dec 4 '06 #2
This might help,

http://www.npsoftware.co.uk/Tutorial...stence_XML.php

Check out Serialization at the bottom of the page... should read
"Serialisation" really...

Nick.

<te********@hotmail.comwrote in message
news:11**********************@f1g2000cwa.googlegro ups.com...
>I would like to serialize a class to XML. The format of the XML is
fixed (see below). It only needs to be serialized - it is read by
another application.

I know I can do it manually using XMLTextWriter - e.g.

Dim xwriter = New
XmlTextWriter("C:\Projects\Formflo\WindowsService\ NDde\test.xml",
System.Text.Encoding.UTF8)
xwriter.Formatting = Formatting.Indented
xwriter.Indentation = 4

xwriter.WriteStartDocument(True)

'Write an element (this one is the root).
xwriter.WriteStartElement("PRODTICKER")
xwriter.WriteStartElement("COUNTERDISPLAY")
xwriter.WriteStartElement("COUNTERITEM")
However, I would like to make it a bit more "elegant" and create a
Serializable class. The actual values in the document
("CURRENTSHIFTDISPLAY", "3471") are coming from a DDE feed (please
don't ask!)

I guess what I'm asking (and I've looked for the answers but can't find
them is:

How do you serialize this to retain the "nestedness" - i.e. the
structure?

How do you serialize repeating lists?

Thanks

Edward

=============REQUIRED XML====================

<?xml version="1.0" encoding="utf-8" ?>
<PRODTICKER>
<COUNTERDISPLAY>
<COUNTERITEM>
<CTITLE>CURRENTSHIFTDISPLAY</CTITLE>
<CVALUE>3471</CVALUE>
</COUNTERITEM>
<COUNTERITEM>
<CTITLE>CURRENTSHIFTTARGETDISPLAY</CTITLE>
<CVALUE>4377</CVALUE>
</COUNTERITEM>
<COUNTERITEM>
<CTITLE>LASTSHIFTDISPLAY</CTITLE>
<CVALUE>8460</CVALUE>
</COUNTERITEM>
<COUNTERITEM>
<CTITLE>SECONDLASTHIFTDISPLAY</CTITLE>
<CVALUE>7146</CVALUE>
</COUNTERITEM>
<COUNTERITEM>
<CTITLE>THIRDLASTSHIFTDISPLAY</CTITLE>
<CVALUE>7354</CVALUE>
</COUNTERITEM>
</COUNTERDISPLAY>
<STATUSLIGHTS>
<STATUSLIGHTITEM>
<LINENUMBER>6</LINENUMBER>
<STATUS>1</STATUS>
<LINEVALUE>366</LINEVALUE>
</STATUSLIGHTITEM>
<STATUSLIGHTITEM>
<LINENUMBER>7</LINENUMBER>
<STATUS>1</STATUS>
<LINEVALUE>2</LINEVALUE>
</STATUSLIGHTITEM>
<STATUSLIGHTITEM>
<LINENUMBER>8</LINENUMBER>
<STATUS>4</STATUS>
<LINEVALUE>467</LINEVALUE>
</STATUSLIGHTITEM>
<STATUSLIGHTITEM>
<LINENUMBER>9</LINENUMBER>
<STATUS>2</STATUS>
<LINEVALUE>790</LINEVALUE>
</STATUSLIGHTITEM>
<STATUSLIGHTITEM>
<LINENUMBER>10</LINENUMBER>
<STATUS>8</STATUS>
<LINEVALUE>427</LINEVALUE>
</STATUSLIGHTITEM>
</STATUSLIGHTS>
</PRODTICKER>

Dec 4 '06 #3

te********@hotmail.com wrote:
[snip]

Thanks for the tips, guys. Got working code and client happy. ish.

Edward

Dec 5 '06 #4
Pah, clients are never happy! Nice one.

<te********@hotmail.comwrote in message
news:11**********************@79g2000cws.googlegro ups.com...
>
te********@hotmail.com wrote:
[snip]

Thanks for the tips, guys. Got working code and client happy. ish.

Edward

Dec 7 '06 #5

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

Similar topics

14
by: vince | last post by:
Can I add (append) to an xml file that already contains a serialized object, and be able to deserialize to either or both objects from the same file...??? How is this done...?? thanks, vince
5
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream...
3
by: MAY | last post by:
Hi, I have a problem about serialize the form controls. I wrote a test program to test serialize a from but fail (->An unhandled exception of type...
5
by: objectref | last post by:
Hi to all, let's say i have a form with 2 buttons and 3 textboxes on it. Is there a way to Serialize the form so when i de-Serialize and cast it to a Form, i will get back the original form with...
18
by: yusuf | last post by:
I basically want to be able to store and retrieve a tree structure in greasemonkey. Unfortunately the GM_setValue and GM_getValue functions only take string key value pairs. Is there a native...
1
by: Rick Luckwell | last post by:
I have 3 collections(reports, services, charts) of objects(report, service, chart) that are nested with each other. When I serialize the object the output only contains reports and services but...
1
by: davebaranas | last post by:
I am able to serialize this but I get a null exception when I try to deserialize it back Even if I don't make any child classes it throws "Object reference not set to an instance of an object."...
7
by: powellgg | last post by:
I've just taken over a PHP website and am converting it to ASP.NET (don't shoot!). I'm not a PHP guy so I'm doing a lot of searching for things that I aren't obvious, and I'm hoping I'll be able...
4
by: =?Utf-8?B?Qnlyb24=?= | last post by:
When I try to serialize an instance of the LocationCell below (note Building field) I get an error in the reflection attempt. If I remove the _Building field it serializes fine. I tried renaming...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.