469,621 Members | 1,821 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,621 developers. It's quick & easy.

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 2052
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

14 posts views Thread by vince | last post: by
5 posts views Thread by David Sworder | last post: by
3 posts views Thread by MAY | last post: by
5 posts views Thread by objectref | last post: by
18 posts views Thread by yusuf | last post: by
1 post views Thread by Rick Luckwell | last post: by
1 post views Thread by davebaranas | last post: by
7 posts views Thread by powellgg | last post: by
4 posts views Thread by =?Utf-8?B?Qnlyb24=?= | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.