Couple of XML Issues, Please Help..........

Bilalq

Member
Joined
Jul 21, 2004
Messages
24
Programming Experience
3-5
Hello All,
I am using the XMLTextWriter to create a XML file for a client. The data is coming from the database for the file. I have couple of issues that I need to resolve, but not sure how to do that.

1. If the field has no data the XMLTextWriter will write the element as
<Name /> instead of <Name> </Name>. I know they both mean
the same, but the client requires it in the form <Name> </Name>
format.

2. When the XML file is generated, the very first line is written as
<?xml version="1.0"?>, I would like the encoding in there as well. For
Example the line should read <?xml version="1.0" encoding="utf-8" ?>.

Here is the start of the code.......

VB.NET:
xmlWriter = New XmlTextWriter(xmlFile, Nothing)
 
'set the formatiing for the xml file
xmlWriter.Formatting = Formatting.Indented
xmlWriter.Indentation = 4
xmlWriter.IndentChar = " "c
 
xmlWriter.WriteStartDocument()
 
'Write the Root Node
xmlWriter.WriteStartElement("File")
'write the attribute for the file element
xmlWriter.WriteAttributeString("xmlns",".....")
 
'Write First Node
xmlWriter.WriteStartElement("Filer_Information")
'Write the attribute for the file_Information
xmlWriter.WriteAttributeString("xmlns", ".......")
xmlWriter.WriteElementString("Date", Format(Now, "yyyyMMddhhmmss"))
xmlWriter.WriteEndElement()
 
'Write Reports Node
xmlWriter.WriteStartElement("Reports")
'
'
'
Any help would be appreciated. Thanks in advance.
 
Last edited by a moderator:
1. A little whitespace can't hurt, and have to be done to get both open and close tag explicitly:
VB.NET:
xmlWriter.WriteStartElement("Name")
xmlWriter.WriteString(" ")
xmlWriter.WriteEndElement()
2. Like doc says, "If encoding is a null reference (Nothing in Visual Basic) it writes out the stream as UTF-8 and omits the encoding attribute from the ProcessingInstruction."
VB.NET:
xmlWriter = New XmlTextWriter(xmlFile, System.Text.Encoding.UTF8)
 
Thanks for your help on both the issues. There is still a slight problem with the first issue, I don't know ahead of time if it is going to be blank, secondly can't have a space in there.


Thanks for you help
 
Back
Top