|
xxx
Using XMLtextWriter objects to Write XML Data
by Charles Carroll
The XMLTextWriter object helps us
write an XML file without having to worry about quotes, indenting, <, >, etc.
Below is code to write an XML File with an XMLTextWriter object.
It basically transforms:
/learn/test/mb.txt
INTO
http://www.learnasp.com/experiments/data/mbcreatedbyus.xml
yet notice the program itself merely using the various XMLTextwriter methods and
it takes care of indenting and ensuring a valid document. The XMLTextWriter
enforces valid documents. If you call methods in a sequence that would make
invalid XML it does throw errors.
filename=/experiments/xmltextwriter/mb.aspx
<%@ trace="true" debug="true"%>
<%@ import namespace="system.io"%>
<%@ import namespace="system.xml"%>
<Script Language="vb" runat="server">
Sub Page_Load(s as object, e as eventargs)
' Necessary to Parse the mb.tx file
Dim fs as FileStream
Dim sr as StreamReader
Dim strLine as String
Dim xLoop as Integer
Dim strType1 as string, strType2 as string,strQuestion, strAnswers as String
Dim aryAnswers as Array
Dim intQnum as Integer
Dim strPathOfSite as string
' XML writing stuff
DIM xmltw1 as XmlTextWriter
TRY
strPathOfSite=request.PhysicalApplicationPath()
trace.write("strPathOfSite",strPathOfSite)
fs=new FileStream(strPathOfSite & "\data\mb.txt", filemode.open )
sr=new StreamReader(fs)
strLine = sr.readline()
xmltw1=new XMLtextWriter(strPathOfSite & "\data\mbcreatedbyus.xml",encoding.utf8)
' xmltw1.StartDocument(true) ' true refers to standalone
xmltw1.Formatting=Formatting.Indented
xmltw1.Indentation=8
xmltw1.WriteStartDocument()
xmltw1.WriteStartElement("MB")
Do UNTIL strLine is Nothing
xmltw1.WriteStartElement("item")
' Parsing
intqNum += 1
strType1=strLine.SubString(0,1)
strType2=strLine.SubString(1,1)
strQuestion = strLine.SubString(3, strLine.IndexOf("-") -3)
strAnswers = strLine.SubString(Len(strQuestion) + 5 )
aryAnswers = strAnswers.Split("/")
trace.write("strType1=" & strType1)
trace.write("strType2=" & strType2)
trace.write("strQuestion=" & strQuestion)
trace.write("aryAnswers(0)=" & aryAnswers(0))
trace.write("aryAnswers(1)=" & aryAnswers(1))
strLine = sr.readline()
' Parsing Done
xmltw1.WriteElementString("question",strQuestion)
xmltw1.WriteStartElement("answer")
xmltw1.WriteAttributeString("code",strType1)
xmltw1.WriteString(aryAnswers(0))
xmltw1.WriteEndElement() ' answer
xmltw1.WriteStartElement("answer")
xmltw1.WriteAttributeString("code",strType2)
xmltw1.WriteString(aryAnswers(1))
xmltw1.WriteEndElement() ' answer
xmltw1.WriteFullEndElement ' item
Loop
xmltw1.WriteFullEndElement ' MB
xmltw1.WriteEndDocument()
CATCH exc1 as exception
Page.controls.add(new literalcontrol(exc1.ToString()))
FINALLY
xmltw1.Flush()
xmltw1.Close()
IF NOT sr IS NOTHING
sr.Close()
END IF
END TRY
End Sub
</script>
<form runat="server">
<ASP:Placeholder id="NewControls" runat="server"/>
</form>
 |  |  |
 |
There are many worthy charities!!. But perhaps help starving children in Africa or South America AND help Charles too.
a $5 tip buys him lunch at McDonalds,
a $20 tip buys his kid Hitoshi a new computer game,
a $39 tip buys his daughter Michiko a few nice outfits.
See our donor list.
|  |
 |  |  |
|
|
|
|