Contrary to the answers here, you DON'T need to worry about encoding if the bytes don't need to be interpreted!
Like you mentioned, your goal is, simply, to "get what bytes the string has been stored in".
(And, of course, to be able to re-construct the string from the bytes.)
For those goals, I honestly do not understand why people keep telling you that you need the encodings. You certainly do NOT need to worry about encodings for this.
Just do this instead:
static byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length * sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
// Do NOT use on arbitrary bytes; only use on GetBytes's output on the SAME system
static string GetString(byte[] bytes)
{
char[] chars = new char[bytes.Length / sizeof(char)];
System.Buffer.BlockCopy(bytes, 0, chars, 0, bytes.Length);
return new string(chars);
}
As long as your program (or other programs) don't try to interpret the bytes somehow, which you obviously didn't mention you intend to do, then there is nothing wrong with this approach! Worrying about encodings just makes your life more complicated for no real reason.
Additional benefit to this approach: It doesn't matter if the string contains invalid characters, because you can still get the data and reconstruct the original string anyway!
It will be encoded and decoded just the same, because you are just looking at the bytes.
If you used a specific encoding, though, it would've given you trouble with encoding/decoding invalid characters.
As the other answers say, you need to provide an XML Schema document for your custom configuration section. There's no need to add the .xsd
schema file to some global directory; you can reference it directly from your custom section in the App.config
file:
<configuration>
<!-- make the custom section known to .NET's configuration manager -->
<configSections>
<section name="customSection" type="..." />
</configSections>
<!-- your custom section -->
<customSection xmlns="http://tempuri.org/customSection.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="customSection.xsd">
...
</customSection>
<configuration>
The xmlns
attribute is merely there to set a default namespace, so that you don't need to set it on your customSection
element and all of its child elements. (However, do not place the xmlns
attribute on the <configuration>
element!)
The customSection.xsd
contains the schema that will be used by IntelliSense, for example:
<xs:schema id="customSectionSchema"
targetNamespace="http://tempuri.org/customSection.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/customSection.xsd"
xmlns:mstns="http://tempuri.org/customSection.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="customSection">
...
</xs:element>
</xs:schema>
Best Answer
XML Intellisense will not automatically work for a custom configuration section.
Visual Studio may report warnings on compilation complaining that the attributes of the custom configuration section are not defined. These warnings may be ignored.
If you want XML IntelliSense support for a custom configuration section (or if you just want the 'schema not found' warnings to disappear), add the following line to your DotNetConfig.xsd file immediately after the first <xs:schema ...> line (which is typically the second line in the DotNetConfig.xsd file).
The DotNetConfig.xsd file can be found in your Visual Studio 8 (or 9) installation directory in the Xml\Schemas subdirectory.