|
Looking at msdn documenation: http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.createxmldeclaration.aspx[^]
The section about encoding says,
"The value of the encoding attribute. This is the encoding that is used when you save the XmlDocument to a file or a stream; therefore, it must be set to a string supported by the Encoding class, otherwise Save fails. If this is nullNothingnullptra null reference (Nothing in Visual Basic) or String.Empty, the Save method does not write an encoding attribute on the XML declaration and therefore the default encoding, UTF-8, is used.
Note: If the XmlDocument is saved to either a TextWriter or an XmlTextWriter, this encoding value is discarded. Instead, the encoding of the TextWriter or the XmlTextWriter is used. This ensures that the XML written out can be read back using the correct encoding. "
So I would guess that the "note" applies in your case. Your StringWriter that you are saving to is causing the encoding value to be ignored. (I imagine that the underlying StringBuilder is using UTF-16 strings)
If you were to use the XmlTextWriter, then you can specify the encoding that you want.
|
|
|
|
|
Great wmba!
I studied it. And I think the following statements applies to my issue, right?
--------------------
Note: If the XmlDocument is saved to either a TextWriter or an XmlTextWriter, this encoding value is discarded. Instead, the encoding of the TextWriter or the XmlTextWriter is used. This ensures that the XML written out can be read back using the correct encoding. "
--------------------
But It only mentions TextWriter and XmlTextWriter, which will be able to use their own encoding approach, but I am using StringWriter, it is not mentioned in the document, right?
regards,
George
|
|
|
|
|
|
Thanks wmba,
1.
I have solved this issue from your help. Here is my code. Could you review whether it is correct please?
using System;
using System.Text;
using System.IO;
using System.Xml;
class FSOpenWrite
{
public static void Main()
{
StringWriter stream = new StringWriter();
XmlTextWriter writer = new XmlTextWriter(stream);
writer.WriteStartElement("Stock");
writer.WriteAttributeString("Symbol", "123");
writer.WriteElementString("Price", "456");
writer.WriteElementString("Change", "abc");
writer.WriteElementString("Volume", "edd");
writer.WriteEndElement();
string content = stream.ToString();
return;
}
}
2.
Why in my original code in question, even if I set UTF-16, but I can only use UTF-8 encoding?
regards,
George
|
|
|
|
|
With your code sample, you are missing the part to tells the XmlTextWriter what encoding to use. If you use any class that is derived from a TextWriter (like StringWriter), then you can't specify the encoding. The reason for this is that the base string in a StringWriter is UTF-16, so you have no options for using a different Encoding.
If however, you use a MemoryStream, or something derived directly from Stream, then you can specify a different Encoding.
Anyway, here is a code snippet that describes this:
MemoryStream ms = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(ms, Encoding.UTF8);
writer.Formatting = Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("Stock");
writer.WriteAttributeString("Symbol", "123");
writer.WriteElementString("Price", "456");
writer.WriteElementString("Change", "abc");
writer.WriteElementString("Volume", "edd");
writer.WriteEndElement();
writer.Flush();
ms.Seek(0, SeekOrigin.Begin);
StreamReader sr = new StreamReader(ms);
string content = sr.ReadToEnd();
Console.WriteLine(content);
return;
It is important to note that you could have used a similar technique in your original code when you used the XmlDocument.
The reason why you were getting the UTF-16 encoding is because your underlying writer class was a string. StringWriter writes directly to a string (or possibly a StringBuilder). And because strings in .NET are all UTF-16, that is the encoding you got.
When you write directly to a stream (FileStream, MemoryStream, etc), then you are not writing to a string, but conceptually you are writing to just an array of bytes. Because of that you can specify a different encoding.
Anyway, I hope this helps you out.
|
|
|
|
|
I like your sample, wmba!
So, cool!!
regards,
George
|
|
|
|
|
George_George wrote: I am using StringWriter
That doesn't write to a file, does it?
Always use an XmlTextWriter for writing XML documents to files.
|
|
|
|
|
Thanks PIEBALDconsult,
I only need a memory representation (string) for XML. No need to write to a file. My question is, why even if I set UTF-8 property, but in my original question and code, UTF-16 header is displayed?
regards,
George
|
|
|
|
|
I'm guessing that it's because .net strings are two-byte Unicode, but I could easily be wrong.
|
|
|
|
|
Thanks PIEBALDconsult,
I agree C# is using UTF-16 as internal encoding approach, but why the XML header UTF-8 which is already set is overwritten by UTF-16?
regards,
George
|
|
|
|
|
Because doing otherwise would be wrong.
What problem are you trying to solve?
|
|
|
|
|
Thanks PIEBALDconsult,
I do not quite understand why I set UTF-8 header, but UTF-16 is output in my original sample. What is the internal operations which steals and changes my original header?
regards,
George
|
|
|
|
|
The XmlDocument.Save and XmlTextWriter operation will only write well-formed XML.
It knows that the StringWriter uses UTF-16 so it sets the proper encoding.
Encoding in UTF-16, but saying it's UTF-8 would yield mal-formed XML.
If you want UTF-8, write it to a file, a StringBuilder won't do it.
|
|
|
|
|
Can I set the encoding of StringWriter from UTF-16 to UTF-8?
regards,
George
|
|
|
|
|
NO, goddammit! You can't! .net strings are UTF-16, and that's it, end of story!
|
|
|
|
|
Thanks PIEBALDconsult,
I have solved this issue by using MemoryStream.
regards,
George
|
|
|
|
|
Hey guys
im trying to write a crystal report... the problem is that the data source comes from dataGridView.DataSource...
it moans "DataTable already belongs to another DataSet"... i'm thinking i need to send it by value(create a copy that doesn't belong to anything) instead of just passing it to the constructor of the reportViewerForm which is by reference... right?
if my assumtions are correct, how do i send (DataTable)dataGridView.DataSource the reportViewerForm by value?
ive got this now
FrmReportViewer frmReportViewer = new FrmReportViewer((DataTable)dataGridView.DataSource);
thanx
Harvey Saayman - South Africa
Junior Developer
.Net, C#, SQL
think BIG and kick ASS
you.suck = (you.passion != Programming)
|
|
|
|
|
Haven't worked with CR, so I can't tell you how to fix that error - but I can tell you that you cannot pass objects by value in C#. At best, you can clone them and pass the clone.
Cheers,
Vikram.
The hands that help are holier than the lips that pray.
|
|
|
|
|
I need to use SHGetFileInfo to display large icons.For small icon display it works fine. If i change the parameter to display large icon (SHGFI_LARGEICON) it does not display the file with large icon, it always displays small icon. Could you pls help me
hImgSmall = Win32.SHGetFileInfo(fName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON);
hImgLarge = Win32.SHGetFileInfo(fName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON]);
|
|
|
|
|
Have you defined the constants correctly?
public const uint SHGFI_ICON = 0x100;
public const uint SHGFI_LARGEICON = 0x0; // 'Large icon
public const uint SHGFI_SMALLICON = 0x1; // 'Small icon
Dave
|
|
|
|
|
After a bit of 'googling' is appears that SHGetFileInfo ... _LARGEICON returns the 32x32 icon, not any greater than that.
You need to call SHGetFileInfo to get the image list index, then SHGetImageList to get the larger icons.
Some code here[^] that may be useful.
Dave
|
|
|
|
|
I have a data grid that contains a checkbox in its one of the column. I have another column that needs to get populated with current date after we check the checkBox, as soon as we click on checkBox but not after we leave the CheckBox cell.Please help help me.
Thank you.
|
|
|
|
|
|
I tried every events but it gets populated only after we leave the cell after we click on it.Actually I want it to get populated right after we click on the checkbox.
Thank u.
|
|
|
|
|
Hi all,
I'm having a hard time dealing with some errors while using multiple threads to access a local MS SQL Express database.
I will try to explain what my programm does:
1.
There is a socket connection between this program(A) and an other program(B). Depending on the information received from B this program will query the database and update some values.
2.
There is also a thread running who checks the database, looks for updates, and sends them to via the socket to programB.
3.
Then there is also a button on the GUI who send custom commands to programmB. ProgramB will respond to that by sending data back to my program. (like section 1)
Depending on the amount of database access required I receive some errors like:
"System.InvalidOperationException: ExecuteReader requires an open and available Connection. The connection's current state is closed"
or
"There is already an open DataReader associated with this Command which must be closed first"
or
"The connection was not closed. The connection's current state is open"
If have been playing for a while and changing the code, but nothing helped. I guess it has something to do whith to different threads want to acces the same connection at the same time. Can anyone help me on this?
Thanks in advance!
Michiel
|
|
|
|
|