vb.netxmltextwriter

New line in xml with XmlTextWriter


i have this code :

Dim _xml As New XmlTextWriter(_percorso, System.Text.Encoding.UTF8)
_xml.Formatting = Formatting.Indented
_xml.Indentation = 7
_xml.WriteStartDocument()
_xml.WriteStartElement("DespatchAdvice")
_xml.WriteAttributeString("xmlns","urn:oasis:names:specification:ubl:schema:xsd:DespatchAdvice-2")
_xml.WriteAttributeString("xmlns:cac","urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")

I need that the last line with "xmlns:cac..." appare in my xml in a new line.

i try to use _xml.Settings.NewLineChars =vblf or add & vblf to the end of WriteAttributeString but doesn't it works. Looking for a solution i found that XmlTextWriter isn't the better solution but i have lots of code already written using it :-(


Solution

  • If the XmlTextWriter is telling you not to do it, I'd tend to believe it, although I tested the output shown later with one online XML validator and it said it was OK.

    For new code, such as this, you should use an XmlWriter. There's nothing wrong with using newer technology.

    You can sneak round behind the XmlTextWriter by writing to the underlying stream, something like this:

    Dim ms As New MemoryStream()
    Dim x As New XmlTextWriter(ms, System.Text.Encoding.UTF8) With {.Formatting = Formatting.Indented}
    
    Dim crlf = Encoding.Unicode.GetBytes(vbCrLf)
    
    x.WriteStartDocument()
    x.WriteStartElement("DespatchAdvice")
    x.WriteAttributeString("xmlns", "urn:oasis:names:specification:ubl:schema:xsd:DespatchAdvice-2")
    
    'TODO: Don't write to the underlying stream.
    x.Flush()
    ms.Write(crlf)
    
    x.WriteAttributeString("xmlns:cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")
    x.WriteEndElement()
    x.WriteEndDocument()
    x.Flush()
    
    ms.Position = 0
    Dim sr As New StreamReader(ms)
    Dim myStr = sr.ReadToEnd()
    x.Close()
    
    Console.WriteLine(myStr)
    
    Console.ReadLine()
    

    Outputs:

    <?xml version="1.0" encoding="utf-8"?>
    <DespatchAdvice xmlns="urn:oasis:names:specification:ubl:schema:xsd:DespatchAdvice-2"
     xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" />
    

    To write to a file:

    Dim percoso = "C:\temp\123.xml" ' for example.
    
    Using fs As New FileStream(percoso, FileMode.Create, FileAccess.Write)
        Dim x As New XmlTextWriter(fs, Encoding.UTF8) With {.Formatting = Formatting.Indented}
    
        Dim crlf = Encoding.Unicode.GetBytes(vbCrLf)
    
        x.WriteStartDocument()
        x.WriteStartElement("DespatchAdvice")
        x.WriteAttributeString("xmlns", "urn:oasis:names:specification:ubl:schema:xsd:DespatchAdvice-2")
    
        'TODO: Don't write to the underlying stream.
        x.Flush()
        fs.Write(crlf)
    
        x.WriteAttributeString("xmlns:cac", "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2")
        x.WriteEndElement()
        x.WriteEndDocument()
        x.Flush()
        x.Close()
    
    End Using