Reputation: 533
I have an xml file where I need "&" to be printed for a checkbox name in a form. But the "&" is not printed. The notation I have used is &
. But in this case it is not printed. How can I do this, Is there any other way to represent? The XML format is mentioned below.
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE Strings[<!ELEMENT Strings ANY><!ELEMENT String ANY><!ATTLIST String Name ID #REQUIRED>]> <Strings> <String Name="0">&</String> </Strings>
The code snippet is below,
public string Load(string _strShortName)
{
StreamReader reader = null;
string l_strShortname = _strShortName;
try
{
xmlCurrentLanguage = new XmlDocument();
//added for the fix and commented
if (File.Exists(strPath + "lang_" + _strShortName + ".xml"))
{
reader = new StreamReader(File.OpenRead(strPath + "lang_" + _strShortName + ".xml"));
}
else
{
//hot fix language change issue.
reader =new StreamReader(File.OpenRead( strPath + "lang_en.xml"));
}
xmlCurrentLanguage.PreserveWhitespace = true;
xmlCurrentLanguage.Load(reader);
reader.Close();
reader.Dispose();
}
catch (Exception e)
{
if (reader != null)
{
reader.Close();
reader.Dispose();
}
MessageBox.Show(e.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
return l_strShortname;
}
public string GetString(string _id)
{
// Versuche eine zu laden
XmlNode node = null;
if (xmlCurrentLanguage != null)
{
node = xmlCurrentLanguage.GetElementById(_id);
if (node != null)
{
return Parse(node.InnerText);
}
}
return "";
}
private string Parse(string str)
{
string parsedStr;
parsedStr = str.Replace("\n", System.Environment.NewLine);
parsedStr = str.Replace("%n", System.Environment.NewLine);
return parsedStr;
}
public string GetText(string _id, params string[] _cmd)
{
string str = GetString(_id);
string origError = GetString(_id + "_O");
if (origError != "")
{
for (int i = 0; i < _cmd.Length; i++)
{
str = _cmd[i].Replace(origError, str);
}
}
else
{
for (int i = 0; i < _cmd.Length; i++)
{
str.Replace("%" + (i + 1), _cmd[i]);
}
}
return str;
}
In the form,
public void FillUICaption(ref Language currentLanguage)
{
chkbox.Text = language.TXT_DEFAULT_KET;
}
The code to access the value,
public string TXT_DEFAULT_KET{ get { return GetString("1000"); } }
Upvotes: 0
Views: 249
Reputation: 14608
You can use &
to represent &
sign in the xml. It will be read as a &
by almost every Xml parser.
Update: Your xml file should look like this:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Strings[<!ELEMENT Strings ANY><!ELEMENT String ANY><!ATTLIST String Name ID #REQUIRED>]>
<Strings>
<String Name="0">&</String>
</Strings>
Update: The reason you cannot find the element is because you are using this code to access the value:
public string TXT_DEFAULT_KET{ get { return GetString("1000"); } }
No element with such String ID (i.e. Name) exists. Replace it with the following to get what you want.
public string TXT_DEFAULT_KET{ get { return GetString("0"); } }
BTW, you actually can't display only &
char in a windows form checkbox as it is used for highlighting the hotkey. Use &&
to display it.
Upvotes: 1
Reputation: 2719
See here Usually xml based on encoding="UTF-8" and it supports special character only by using numeric character representation.
Here you case for Ampersand & or &(which is entity reference)
Upvotes: 0