amateur
amateur

Reputation: 44673

convert string in to encoded xml document

I am working with vb.net and have this long string which contains an xml document. ie. the content of the string is xml.

Is it possible to encode this string as valid utf-8 encoded xml document? How could I do this?

Unfortunately the created string is done with string concatenation with no encoding on the nodes values etc, I am attempting to clean such up and need to ensure its encoded correctly and a valid xml document.

Upvotes: 0

Views: 3354

Answers (2)

Alireza Mn
Alireza Mn

Reputation: 75

suppose you have an xml document containing this:

<?xml version="1.0" encoding="utf-8"?>
<xml>
  <myfriends>
  </myfriends>
</xml>

and you have a string containing xml like this:

dim mystring as string="<single_friend><id>21</id></single_friend>"

now to add this to your xml document you can do this:

        Dim myxml As XElement = XElement.Parse(mystring)
        document.Root.Element("myfriends").Add(myxml) 
        document.Save(path)

and your final xml document will be like:

<?xml version="1.0" encoding="utf-8"?>
<xml>
  <myfriends>
    <single_friend>
      <id>21</id>
    </single_friend>
  </myfriends>
</xml>

Upvotes: 0

Pawel
Pawel

Reputation: 31620

This should be able to do the trick: XDocument.Parse()

Upvotes: 2

Related Questions