I have this code for the settings:
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
settings.OmitXmlDeclaration = True
settings.NewLineOnAttributes = True
Then I have this code for the writer:
Dim xml As New XmlTextWriter(Server.MapPath("output.xml"), enc)
Please can you tell me how I make the settings apply to the writer?
Thanks a lot,
Phil.
EDIT: Code sample
Sub writexml_OnClick(ByVal sender As Object, ByVal e As EventArgs)
Try
'Vars
Dim securityid As String = Input_securityid.Text
Dim enc As Encoding
Dim settings As XmlWriterSettings = New XmlWriterSettings()
settings.Indent = True
settings.OmitXmlDeclaration = True
settings.NewLineOnAttributes = True
settings.Encoding = enc
'Declare the writer and set file name / settings
Dim xml As XmlWriter = XmlWriter.Create(Server.MapPath("output.xml"), settings)
'start document
xml.WriteStartDocument()
xml.WriteComment("")
'start envelope
xml.WriteStartElement("soap", "Envelope", "http://schemas.xmlsoap.org/soap/envelope/")
'start body
xml.WriteStartElement("soap", "Body", Nothing)
xml.WriteAttributeString("xmlns", "ns1", Nothing, "http://its/foo.wsdl")
'start biographical capture
xml.WriteStartElement("ns1:biographicalcaptureElement")
'start securityid
xml.WriteStartElement("ns1:securityid")
xml.WriteValue(securityid)
'end securityid
xml.WriteEndElement()
'start requestdata
xml.WriteStartElement("ns1:requestdata")
'end requestdata
xml.WriteEndElement()
'end biographical capture
xml.WriteEndElement()
'end body
xml.WriteEndElement()
'end envelope
xml.WriteEndElement()
'end document
xml.WriteEndDocument()
'clean up
xml.Flush()
xml.Close()
Catch ex As Exception
errorlbl.Text = ex.ToString
Finally
errorlbl.Text = ("Created file ok")
End Try
End Sub
It does work fine if I use;
Dim xml As New XmlTextWriter(Server.MapPath("output.xml"), enc)
the xml is produced but settings are not applied.
This won’t get you an
XmlTextWriter, but to be honest I’ve always usedXmlWriterwhen writing to a file anyway (XmlWriteris the base class ofXmlTextWriter.)You can use
XmlWriter.Create(Server.MapPath("output.xml"), settings)which will give you anXmlWriterinstead of anXmlTextWriter. Your encoding will then need to be set in your settings instance (settings.Encoding = enc.)EDIT:
The sample code provided for me produces:
EDIT 2:
Your namespace is causing a problem because it’s trying to put the element name as
ns1:securityidwhen it should be the element name issecurityidand the namespacens1. You’ll need to separate these like you’ve done in theWriteAttributeStringcall, like so:instead of:
xml.WriteStartElement("ns1:biographicalcaptureElement")use:
xml.WriteStartElement("biographicalcaptureElement", "ns1")With these changes in place I now get: