MAC
MAC

Reputation: 686

How to write xml with data inside the lesser/greater than symbol?

I have this sample xml file

<LTRP cd="1">
  <Customize>0</Customize>
  <Gud>0</Gud>
  <Kingaku>64500000</Kingaku>
  <Kingaku term="1" year="2017" month="0">0</Kingaku>
</LTRP>

and I got this code for that - providing that I already have the data needed

        Dim settings As XmlWriterSettings = New XmlWriterSettings()
        settings.Indent = True

        Dim dsProperty As New DsikopgmProperty
        Dim xmlFileName As String = "filename"
        ' Create XmlWriter.
        Using writer As XmlWriter = XmlWriter.Create("C:\FILES\" + xmlFileName, settings)
            ' Begin writing.
            writer.WriteStartDocument()

            For Each record In records
                writer.WriteStartElement("LTRP cd", dsrecords.BunruiCd)
                writer.WriteElementString("Customize", "")
                writer.WriteElementString("Gud", "0")
                writer.WriteEndElement()
            Next

            ' End document.
            writer.WriteEndElement()
            writer.WriteEndDocument()
        End Using

I would want to know what particular part of my code should I change to have these output

<LTRP cd="1">
</LTRP>

<Kingaku term="1" year="2017" month="0">0</Kingaku>

While I'm at it, can anyone also tell me what do we call the <> symbol other than greater than and lesser than?

Upvotes: 0

Views: 160

Answers (1)

MAC
MAC

Reputation: 686

This is the modification I made to come up with the output desired

    Dim writer As XmlWriter = Nothing

    writer = XmlWriter.Create("sampledata.xml")
    writer.WriteStartElement("LTRP")
    writer.WriteAttributeString("cd", "1")

    writer.WriteElementString("Customize", "0")
    writer.WriteElementString("Gud", "0")

    writer.WriteElementString("Kingaku", "64500000")
    writer.WriteStartElement("Kingaku")
    writer.WriteAttributeString("term", "1")
    writer.WriteAttributeString("year", "2017")
    writer.WriteAttributeString("month", "0")
    writer.WriteValue("0")
    writer.WriteEndElement()
    writer.WriteElementString("rate", "10")
    writer.WriteEndElement()

    writer.Flush()
    writer.Close()

For the source, click here. Thanks to @the_lotus for that significant comment.

Note: I only used constant values for testing. It could be replaced with variables to loop many records.

Upvotes: 1

Related Questions