472,337 Members | 1,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,337 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 2151
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...
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...
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...
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...
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...
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...
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...
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...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.