Connecting Tech Pros Worldwide Help | Site Map

How to add xsi:nil="true" to an XML document node using vb.net

Newbie
 
Join Date: Jun 2008
Location: Lake City, FL
Posts: 1
#1: Jun 2 '08
I'm fairly new to vb.net (self-taught and realizing how much I DON'T know). I need to create an XML output file (I'm quite ignorant in this area as well!). I used an XMLDataDocument and Xml.XmlTextWriter, creating each element node since the agency I'm sending the file to is very specific about the format.

How do I specify an element/node/attribute (whatever) to output xsi:nil="true"?


Desired Output:
<D02 xsi:nil="true" />


Current Output:
<D02 xsi:nil="" xmlns:xsi="true" />

Visual Basic Code:
Function Create_XML_File(ByVal xmlObject As MyObject, _
ByRef ioXmlDoc As Xml.XmlDataDocument) As Integer
Dim ioRootNode As Xml.XmlNode
Dim ioSubNode As Xml.XmlNode
Dim ioDataNode As Xml.XmlNode
Dim ioObject As New MyProject.MyObject

ioObject = xmlObject

' ------------- Dataset Root Node -------------------------------------
ioRootNode = ioXmlDoc.CreateNode(Xml.XmlNodeType.Element, "MyDataSet", "")
ioXmlDoc.AppendChild(ioRootNode)

' ------------- Header Node -------------------------------------
ioSubNode = ioXmlDoc.CreateNode(Xml.XmlNodeType.Element, "Header", "")
ioRootNode.AppendChild(ioSubNode)

' ------------- Data Elements -------------------------------------
ioDataNode = ioXmlDoc.CreateNode(Xml.XmlNodeType.Element, "D01", "")
ioDataNode.InnerText = ioObject.D01
ioSubNode.AppendChild(ioDataNode)

ioDataNode = ioXmlDoc.CreateNode(Xml.XmlNodeType.Element, "D02", "") ioDataNode.Attributes.Append(ioXmlDoc.CreateAttrib ute("xsi:nil", IIf(ioObject.D02 = "", "true",
ioObject.D02)))
ioSubNode.AppendChild(ioDataNode)

~~~~~~~~~~~~~~~

Dim ioWriter As New Xml.XmlTextWriter(cPath & vFileName, System.Text.ASCIIEncoding.ASCII)

' Write XML document object to file
ioWriter.Formatting = Xml.Formatting.Indented
ioWriter.WriteStartDocument()
xmlDoc.WriteContentTo(ioWriter)
ioWriter.Close()

~~~~~~~~~~~~~~~
Reply