Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear everybody
I am new in XML, I want to write a program to write in an xml file in this format:


XML
<?xml version="1.0" encoding="utf-8" ?>  
- <Orders>
  <Item id="1" SKU="998123" Description="Super Widget" Quantity="100" />
  <Item id="2" SKU="81312" Description="Turbo Flangellator" Quantity="50" />
  </Orders>


but when I execute the code the xml file appeared like the following:
XML
- <Orders>
  <Item id="1" SKU="998123" Description="Super Widget" Quantity="100" />
  <Item id="2" SKU="81312" Description="Turbo Flangellator" Quantity="50" />
  </Orders>



what should I do to let it write the encoding line?


my code is:
Dim xmlWriter As New XmlTextWriter("c:\XMLTextWriter.xml", Nothing)

With xmlWriter
    .WriteStartElement("Orders")
    .WriteStartElement("Item")
    .WriteAttributeString("id", "1")
    .WriteAttributeString("SKU", "998123")
    .WriteAttributeString("Description", "Super Widget")
    .WriteAttributeString("Quantity", "100")
    .WriteEndElement()

    .WriteStartElement("Item")
    .WriteAttributeString("id", "2")
    .WriteAttributeString("SKU", "81312")
    .WriteAttributeString("Description", "Turbo Flangellator")
    .WriteAttributeString("Quantity", "50")
    .WriteEndElement()


    .WriteEndElement()
    .Close()
End With
Posted
Updated 15-Sep-10 1:13am
v3
Comments
Dalek Dave 14-Sep-10 4:58am    
Edited for Code Block.

Try this -

xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteStartDocument();

This should get you the encoding line
 
Share this answer
 
Comments
fadi77_net 15-Sep-10 7:08am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Hi
Try this out
Dim enc As New System.Text.UTF8Encoding()
        Dim xmlWriter As New XmlTextWriter("c:\XMLTextWriter.xml", enc)

        With xmlWriter
            .WriteStartDocument()
            
            .WriteStartElement("Orders")
            .WriteStartElement("Item")
            .WriteAttributeString("id", "1")
            .WriteAttributeString("SKU", "998123")
            .WriteAttributeString("Description", "Super Widget")
            .WriteAttributeString("Quantity", "100")
            .WriteEndElement()

            .WriteStartElement("Item")
            .WriteAttributeString("id", "2")
            .WriteAttributeString("SKU", "81312")
            .WriteAttributeString("Description", "Turbo Flangellator")
            .WriteAttributeString("Quantity", "50")
            .WriteEndElement()


            .WriteEndElement()
            .Close()


You can format the XML file using
VB
.Formatting =Formatting.Indented
.Indentation =3


Hope this will help you :)
 
Share this answer
 
Comments
fadi77_net 15-Sep-10 7:08am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
I am using XmlDocument instead of the XmlTextWriter. There, I am using the XmlDocument.CreateProcessingInstruction[^] method for this.

Cheers
Uwe
 
Share this answer
 
Comments
fadi77_net 15-Sep-10 7:08am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900