Serialize object to XML with nested structure

T

teddysnips

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>
 
R

Robinson

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
 
T

teddysnips

(e-mail address removed) wrote:
[snip]

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

Edward
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top