0

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">&#65286;</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"); } }
roopini n
  • 503
  • 2
  • 7
  • 29

2 Answers2

1

You can use &amp; 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">&amp;</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.

Yogesh
  • 14,498
  • 6
  • 44
  • 69
  • "Almost every"? If it doesn't understand `&` then it is not an XML parser. By definition. – Michael Kay Aug 07 '14 at 07:29
  • I agree but there a lot of custom xml parsers out there, which makes me not so sure. Not every programmer follows everything by the book. – Yogesh Aug 07 '14 at 08:24
  • Can you post the code where you are reading this xml? Also, the form code for setting the checkbox will also help. – Yogesh Aug 07 '14 at 09:03
  • Yogesh, you can't produce a parser that doesn't process well-formed XML and call it an XML parser. Yes, I know there are rubbish programmers out there: some of them, and many more of their victims, are on StackOverflow, but can't we agree that when someone says XML, they mean XML? – Michael Kay Aug 07 '14 at 13:48
  • @Michael: I absolutely agree with you. :) – Yogesh Aug 07 '14 at 16:49
  • @Yogesh - Sorry for the string number, yes that is "0" only , but it is not happening. I dint say that I m parsing, It is written by someone else and we using it. Also There should be a way to print this. i am looking for that. – roopini n Aug 08 '14 at 03:08
0

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 &#38; or &amp;(which is entity reference)

Nitin Dominic
  • 2,659
  • 1
  • 20
  • 22