Simon
Simon

Reputation: 133

Write dictionary or string to XML

I am looking to write the contents of a dictionary to XML. I have written the dictionary contents to a string as i have been told it is easier to write from a string to XML instead of from a dictionary. But im not sure if this is correct?

Is there any way to write this to XML from the dictionary or string?

Upvotes: 0

Views: 1713

Answers (1)

competent_tech
competent_tech

Reputation: 44931

Unfortunately, this is a very open-ended question since the format of the desired XML will determine the output.

One very easy way is to roll your own XML:

    Dim sbService As New StringBuilder

    sbService.AppendLine("<?xml version=""1.0""?>")
    sbService.AppendLine("<services>")

    For Each item As KeyValuePair(Of String, Services) In dictservice
        sbService.AppendFormat("  <service DisplayName=""{0}"" ServiceName=""{1}""></service>", item.Key, item.Value.ToString()).AppendLine()
    Next
    sbService.AppendLine("</services>")

This will produce XML similar to the following:

<?xml version="1.0"?>
<services>
  <service DisplayName="Application Experience" ServiceName="AeLookupSvc"></service>
</services>

Upvotes: 1

Related Questions