&amp problem RRS feed

  • Question



    I am using XMLDocument and XmlNode to create XML and to save it. In the nodes, some of the values contain &, some contain ',then " and so on. if i dont change this characters manualy, they will stay as they are, but if I change them, lets say for ' I should get ' and instead I get '. First I change & sign and after this I change all the others. Is there some other way to change these characters?


    Monday, April 14, 2008 7:35 AM


All replies

  • Hi,


    The special XML characters should be correctly encoded (escaped) when you call Save on the XmlDocument. Then if you load them back as an XML (for example into XmlDocument), the XML parser will translate them back to the original characters. Simply put saving and loading the XmlDocument should give you the exact same document as the one you started with.


    I tried this (just to verify):


    Code Snippet

                XmlDocument doc = new XmlDocument();
                XmlElement r = doc.CreateElement("root");
                r.AppendChild(doc.CreateTextNode("my & character"));


                XmlWriterSettings ws = new XmlWriterSettings();
                ws.OmitXmlDeclaration = true;
                ws.Indent = true;
                using (XmlWriter w = XmlWriter.Create(Console.Out, ws)) {


    And the output was (As expected):

    my & character


    Maybe if you could post a little sample of your code and what you're trying to get we would be able to help some more.



    Monday, April 14, 2008 9:26 AM

    But all i nead is to save this XML and my & character to be saved as my & character , not as my  & character
    Monday, April 14, 2008 9:34 AM

    But all i nead is to save this XML and my & character to be saved as my & character , not as my  & character
    Monday, April 14, 2008 9:35 AM

    Sorry - the forum software corrupted my answer. The output which the piece of code I posted above produces is this:


    Code Snippet
    my & character  

    You can give it a try...


    The way I understand it is, that that's exactly what you want - right?



    Monday, April 14, 2008 9:38 AM
  • Exactly  i want to get my & character and instead i'm getting my & character also for the my ' character  I'm getting my ' character  I'm changing it and it seems to be changing it too. here is my code:


    Code Snippet

    XmlDocument doc1 = new XmlDocument();

    doc1.LoadXml("<root><node1 p_id=\"some\"/></root>");

    XmlNode newElem, newElem1;

    XmlAttribute attr;

    String str1;


    foreach (DataGridViewRow dr in dataGridView1.Rows)


    newElem = doc1.CreateNode(XmlNodeType.Element, "NEWELEM", "");


    foreach (DataGridViewCell dc in dr.Cells)


    if (dc.Value != null && !dc.Value.ToString().Equals("") )


    str1 = dc.FormattedValue.ToString().Replace("&", "&amp;");

    str1 = str1.Replace("<", "&lt;");

    str1 = str1.Replace(">", "&gt;");

    str1 = str1.Replace("'", "&apos;");

    str1 = str1.Replace("\"", "&quot;");

    newElem1 = doc1.CreateElement(dc.OwningColumn.Name);

    newElem1.InnerText = str1.TrimEnd();






    XmlDeclaration xmldecl;

    xmldecl = doc1.CreateXmlDeclaration("1.0", null, null);

    XmlElement root = doc1.DocumentElement;

    doc1.InsertBefore(xmldecl, root);



    Do I need to add something? set ome property of the XmlDocument or something else?
    Tuesday, April 15, 2008 5:25 AM
  • Have you tried not to Replace your special characters? In your code try just to replace line

    newElem1.InnerText = str1.TrimEnd();


    newElem1.InnerText = dc.FormattedValue.ToString().TrimEnd();

    and see if it works for you.

    As Vitek said in one of the previous posts - the characters are escaped/encoded automatically when saving XmlDocument. You escape the characters on your own using Replace function (e.g. original '&' becomes '&') and then they are encoded again during serialization (manually escaped & is escaped again and becomes &ampWink


    Take a look at the quick and dirty code snippet I wrote (below you can find the output xml document saved to file):

    Code Snippet


    XmlDocument xmlDoc = new XmlDocument();


    XmlNode xn = xmlDoc.CreateNode(XmlNodeType.Element, "abc", string.Empty);

    xn.InnerText = "&<>'\"";













    Tuesday, April 15, 2008 6:17 AM
  • It was the first thing i tryed when I dot this problem. But strange, when I'm not converting the characters, I get the " ' " Sign as it is, not converted, and I need it to be converted, for example "I'm" I get it as it is, and I need it to be "I&apos;m"

    Wednesday, April 16, 2008 4:44 AM
  • Now I see what you are trying to accomplish. Take a look at this thread: This seems to be the same questions.


    Hope this helps.


    Wednesday, April 16, 2008 6:21 AM
  • Seems like when I'm not changing the chars manualy, the & sign is changed by default to &amp; but  the ' sign is not changed by default with &apos; And when I change these chars in the program, the ' sign is &apos; untill I make to the XmlDocument doc1.Save(...) What can i do to solve this? are there some special parameter to be set?


    Wednesday, April 16, 2008 7:19 AM
  • '&', '<' and '>' characters have to be escaped by default. Otherwise they would make the Xml document invalid. On the contrary &apos; and ' characters are perfectly valid so they do not have to be escapted since the Xml document with any of them would be still a valid Xml document. I am not aware of any switch/parameter to control this behavior.



    Wednesday, April 16, 2008 7:50 AM
  • I finaly solved it, thanks. I kept the manual transformation of the characters and I use XmlTextWriter instead of, here is the link:

    Thursday, April 17, 2008 11:12 AM