XmlTextWriter Text Values
-
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
-
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
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
-
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
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
-
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
-
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.
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
-
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