Add a noNamespaceSchemaLocation using XmlWriter in VB.NET

38 Views Asked by At

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")
3

There are 3 best solutions below

0
Colster On BEST ANSWER

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")```

0
Yitzhak Khabinsky On

Please try the following solution that is using LINQ to XML API. It is available in the .Net Framework since 2007.

It allows to compose a desired XML in one single statement.

VB.NET

Sub Main
    Dim xsi As XNamespace = "http://www.w3.org/2001/XMLSchema-instance"
    Dim xelem As XElement = New XElement("CapCommsStatus", _
        New XAttribute(XNamespace.Xmlns + "xsi", xsi.NamespaceName), _
        New XAttribute(xsi + "noNamespaceSchemaLocation", "CapCommsStatus_v1.xsd"))

    Console.WriteLine(xelem)
End Sub

Output

<CapCommsStatus xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:noNamespaceSchemaLocation="CapCommsStatus_v1.xsd"/>
0
Martin Honnen On

I would suggest e.g.

    Using writer As XmlWriter = XmlWriter.Create(Console.Out, 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", "http://www.w3.org/2001/XMLSchema-instance", "CapCommsStatus_v1.xsd")
    End Using