Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. XmlTextWriter Text Values

XmlTextWriter Text Values

Scheduled Pinned Locked Moved Visual Basic
csharphtmlxmlquestion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rahvyn6
    wrote on last edited by
    #1

    This is my first time with VB.NET and XML, so feeling my way through. In VB6, you could get the text of the xml into a string using the dom.xml. Is there an equivalent in .NET using the XmlTextWriter? I am basically building 2 xml documents, then I need to concatenate them in a string. Is the XmlTextWriter the best way to go to build xml? Thanks

    R O 2 Replies Last reply
    0
    • R rahvyn6

      This is my first time with VB.NET and XML, so feeling my way through. In VB6, you could get the text of the xml into a string using the dom.xml. Is there an equivalent in .NET using the XmlTextWriter? I am basically building 2 xml documents, then I need to concatenate them in a string. Is the XmlTextWriter the best way to go to build xml? Thanks

      R Offline
      R Offline
      rahvyn6
      wrote on last edited by
      #2

      I have the above problem figured out. However, my output is odd. Using the following code: xws = New XmlWriterSettings xws.Indent = True xws.CheckCharacters = True xws.IndentChars = " " xws.NewLineOnAttributes = True xws.Encoding = System.Text.Encoding.UTF8 xws.CloseOutput = False Dim test As New System.Text.StringBuilder 'Create the access request portion Using xw As XmlWriter = XmlWriter.Create(test, xws) xw.WriteStartDocument(False) xw.WriteStartElement("AccessRequest") xw.WriteAttributeString("xml", "lang", Nothing, "en-US") xw.WriteElementString("AccessLicenseNumber", "LicNum") xw.WriteElementString("UserId", "UID") xw.WriteElementString("Password", "PW") xw.WriteEndElement() xw .WriteEndDocument() xw.Flush() xw.Close() End Using Return test.ToString() And the output is as follows: LicNum UID PW I set the encoding version in the xmlWriterSettings, so why is it putting in utf-16? Also, I dont understand why there is a line feed between the AccessRequest and its attribute. Is this normal? Thanks

      1 Reply Last reply
      0
      • R rahvyn6

        This is my first time with VB.NET and XML, so feeling my way through. In VB6, you could get the text of the xml into a string using the dom.xml. Is there an equivalent in .NET using the XmlTextWriter? I am basically building 2 xml documents, then I need to concatenate them in a string. Is the XmlTextWriter the best way to go to build xml? Thanks

        O Offline
        O Offline
        Oskar net
        wrote on last edited by
        #3

        Use XMLTextWriter as posted above, but be carefull joining two XML docs, a well-formed xml doc must have a single root. You can create a XMLDataDocument from the XMLTextWriter and using XPath Functions (like selectnodes) you can join the docs easily. Once joined, get xml-string. XPath functions return nodes by using expressions (i.e. Select("\ROOT\SECOND_LEVEL\") returns all nodes under . With the property InnerText u can access to tag value. Hope it helps

        ----------------- Solo hay 10 tipos de personas, las que saben binario y las que no

        R 1 Reply Last reply
        0
        • O Oskar net

          Use XMLTextWriter as posted above, but be carefull joining two XML docs, a well-formed xml doc must have a single root. You can create a XMLDataDocument from the XMLTextWriter and using XPath Functions (like selectnodes) you can join the docs easily. Once joined, get xml-string. XPath functions return nodes by using expressions (i.e. Select("\ROOT\SECOND_LEVEL\") returns all nodes under . With the property InnerText u can access to tag value. Hope it helps

          ----------------- Solo hay 10 tipos de personas, las que saben binario y las que no

          R Offline
          R Offline
          rahvyn6
          wrote on last edited by
          #4

          That helps yes, thank you. But I am still not understanding why I am getting the utf-16 encoding attribute when I tell it to use utf-8.

          O 1 Reply Last reply
          0
          • R rahvyn6

            That helps yes, thank you. But I am still not understanding why I am getting the utf-16 encoding attribute when I tell it to use utf-8.

            O Offline
            O Offline
            Oskar net
            wrote on last edited by
            #5

            I've tried setting the encoding for TextWriters, Dim test As New System.Text.StringBuilder() Dim twriter As StringWriter = New StringWriter(test) 'twriter.Encoding = Encoding.UTF8 -- Readonly Dim twriter2 As TextWriter = twriter 'twriter2.Encoding = Encoding.UTF8 -- Readonly Dim xmlWriter As XmlWriter = New XmlTextWriter(twriter2) ...no success... It seems that VB Strings do not allow other than utf-16 encoding (mmmm, strings are stored that way...) for "memory-string-writers" Using files and encoding "utf-8" OK. "I dont understand why there is a line feed between the AccessRequest and its attribute. Is this normal?" take a look to xws.NewLineOnAttributes = True

            ----------------- Solo hay 10 tipos de personas, las que saben binario y las que no

            R 1 Reply Last reply
            0
            • O Oskar net

              I've tried setting the encoding for TextWriters, Dim test As New System.Text.StringBuilder() Dim twriter As StringWriter = New StringWriter(test) 'twriter.Encoding = Encoding.UTF8 -- Readonly Dim twriter2 As TextWriter = twriter 'twriter2.Encoding = Encoding.UTF8 -- Readonly Dim xmlWriter As XmlWriter = New XmlTextWriter(twriter2) ...no success... It seems that VB Strings do not allow other than utf-16 encoding (mmmm, strings are stored that way...) for "memory-string-writers" Using files and encoding "utf-8" OK. "I dont understand why there is a line feed between the AccessRequest and its attribute. Is this normal?" take a look to xws.NewLineOnAttributes = True

              ----------------- Solo hay 10 tipos de personas, las que saben binario y las que no

              R Offline
              R Offline
              rahvyn6
              wrote on last edited by
              #6

              "take a look to xws.NewLineOnAttributes = True" Thanks, that was stupidity on my part. So I'm stuck with utf-16 unless I switch away from a string builder/writer. Thanks for the help.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups