Search code examples
xmlvb.netxmlwriter

Add a noNamespaceSchemaLocation using XmlWriter in VB.NET


I have to build an xml as a confirmation of receipt of a postscript file. I have all the code done but the example shows a nonamespace which I cannot get to appear correctly.

The example looks like this: <CapCommsStatus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="CapCommsStatus_v1.xsd">

Nearest I have got to is this: <CapCommsStatus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" p2:noNamespaceSchemaLocation="CapCommsStatus_v1.xsd" xmlns:p2="xsi">

        Dim writersettings As New XmlWriterSettings
        Dim writer
        With writersettings
            .Indent = True
            .IndentChars = vbTab
            .NewLineChars = vbCrLf
            .NewLineHandling = NewLineHandling.Replace
            .Encoding = New UTF8Encoding(False)
        End With
        writer = XmlWriter.Create(outputName, writersettings)
        writer.WriteStartDocument(True)
        writer.WriteStartElement("CapCommsStatus")
        writer.WriteAttributeString("xmlns", "xsi", "http://www.w3.org/2000/xmlns/", "http://www.w3.org/2001/XMLSchema-instance")
        writer.WriteAttributeString("xsi", "noNamespaceSchemaLocation", vbNull, "CapCommsStatus_v1.xsd")

Solution

  • OK so not using .Create gave me the output I was looking for:

     writer = New XmlTextWriter(outputName, System.Text.Encoding.UTF8)
                writer.WriteStartDocument(True)
                writer.Formatting = Formatting.Indented
                writer.WriteStartElement("CapCommsStatus")
                writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
                writer.WriteAttributeString("xsi:noNamespaceSchemaLocation", "CapCommsStatus_v1.xsd")```