Thursday, July 30, 2009

C# XMLTextWriter.WriteStartDocume...

Hi,





I wonder if anyone can tell me how to output the encoding attribute of an XML File using the XMLTextWriter class (I tried using the WriteStartDocument method but it doesn't seem to have the option to include encoding).





Also, I have a problem writing a physical line break. I am using the WriteRaw method to output a vCard and would like each attribute of the vCard to be displayed on a different line of the XML file. I have included a snippet of c# code to show what I am currently writing. Any suggestions would be greatfully appreciated. I have tried WriteString("%26lt;br/%26gt;), WriteString("\n\r"), WriteRaw("\n\r").





private void writeVcard(XmlTextWriter tw, string strFirstName, string strEmailAddress)


{


tw.WriteStartElement("vCard");


tw.WriteRaw("BEGIN:VCARD");


tw.WriteRaw("\n");


tw.WriteRaw("FN:" + strFirstName + " \n");


tw.WriteRaw("EMAIL;TYPE=INTERNET:" + trEmailAddress + " \n");


tw.WriteRaw("END:vCard");


tw.WriteEndElement();

C# XMLTextWriter.WriteStartDocume...
Encoding should be passed to the constructor of the XmlTextWriter before calling this method.


...


XmlTextWriter tw = new XmlTextWriter(


%26lt;your output stream%26gt;,


Encoding.UTF8);


writeVCard(tw,"first","last");


...





Encoding options are listed here:


http://msdn2.microsoft.com/en-us/library...





You also need to use WriteWhitespace method on the XmlTextWriter to generate line breaks.





Instead of:


tw.WriteRaw("\n");





use:


tw.WriteWhitespace("\n");


No comments:

Post a Comment