Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XmlTextWriter

Status
Not open for further replies.

dpursifull

Programmer
Sep 15, 2003
7
US
I am attempting to generate some XML using the XmlTextWriter object for the first time and was wondering how I populate the value of an element. I have looked through all of the different methods associated with this object in the MSDN library and can't quite figure out which one it is. I would greatly appreciate any info.

Thanks!
 
The XmlTextWriter can be used to write XML to a file in a forward-only direction (you need to write the file starting at the first element in your XML and work in tree order to the bottom). Here's a sample from MSDN to show you how it works:
Code:
Public Shared Sub Main()
   Dim writer As New XmlTextWriter(Console.Out)
   writer.Formatting = Formatting.Indented
   WriteQuote(writer, "MSFT", 74.125, 5.89, 69020000)
   writer.Close()
End Sub 'Main

Shared Sub WriteQuote(writer As XmlWriter, symbol As String, price As Double, change As Double, volume As Long)
   writer.WriteStartElement("Stock")
   writer.WriteAttributeString("Symbol", symbol)
   writer.WriteElementString("Price", XmlConvert.ToString(price))
   writer.WriteElementString("Change", XmlConvert.ToString(change))
   writer.WriteElementString("Volume", XmlConvert.ToString(volume))
   writer.WriteEndElement()
End Sub 'WriteQuote
Note a couple of things.

- When creating an instance of the XmlWriter, they are directing the output to the Console. You can replace this with any stream or file (the constructor is overloaded)

- When they add their value to the writer using the WriteElementString method, they're using the static ToString method of the XmlConvert class. This is important, because when you write dates to XML, they must be in ISO-8601 format, and the XmlConvert class does this for you. It also converts ints, doubles, etc. to string for you, so use it every time just to be safe.

- They used the Indented formatting for their XML stream. This is a "nice to have", as XML doesn't care about whether the file is pretty or not -- it just looks for the element tags. You can reduce your XML filesize by leaving this off.

Hope this helps.
Chip H.


If you want to get the best response to a question, please check out FAQ222-2244 first
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top