Escape Characters Question

Feb 4, 2015 at 6:13 PM
Edited Feb 4, 2015 at 6:15 PM
Hello Don,

When I deserialize a name segment (NM1) such as


into the generated classes and then read the string for representing the name using the properties (S_NM1.D_1036_4), it reads as "CHILDREN&apos ;S MEDICAL CENTER". That is the apostrophe is escaped. Is this the intended behavior?

Feb 5, 2015 at 7:36 AM
Hi Tim,

Yes this is the default behavior. As I'm building an xml out of the edi, I use SecurityElement.Escape to make sure the Xml isn't broken.

It would make sense once you want to work on the value to unescape it - I'm using SecurityElement.FromString when converting back to EDI.

Aug 30, 2016 at 11:05 PM
Edited Aug 30, 2016 at 11:09 PM
Hello Don,

I've been working with this library for a over a year and absolutely love it.

However, it still seems buggy to have to unescape a field in the POCO classes after it has already been deserialized from the generated xml.. In my mind, everything should be unescaped at this point.

Take this nm1 segment for example

this will get serialized into "malformed" xml while parsing that looks like this
                  <D_NM104_MemberFirstName>JOHN &amp;quot;JACK&amp;quot;</D_NM104_MemberFirstName>
I assume the way you would have liked the xml to look is like this:
                  <D_NM104_MemberFirstName>JOHN &quot;JACK&quot;</D_NM104_MemberFirstName>

the reason it get's malformed lies in the calls like this: element.SetValue(SecurityElement.Escape(dataElement.Value) ?? string.Empty)
SecurityElement.Escape escapes the string value with the " and then the XmlWriter escapes the value again when writing to xml because it encounters the ampersands in (") and escapes that as (&quote;)

When the xml serializer deserializes the malformed xml into a POCO hierarchy the value for the .....S_NM1_MemberName.D_NM104_MemberFirstName field is "JOHN "JACK"" because the only thing that is escaped in the xml is the ampersand. Once the ampersand is unescaped you have two more escaped characters to contend with.
    static void Main(string[] args)
        XElement elem = new XElement("D_NM104_MemberFirstName");
        elem.SetValue("JOHN \"JACK\"");

        elem.SetValue("JOHN <JACK>");

        elem.SetValue("JOHN & JACK");
will output
<D_NM104_MemberFirstName>JOHN "JACK"</D_NM104_MemberFirstName>
<D_NM104_MemberFirstName>JOHN &lt;JACK&gt;</D_NM104_MemberFirstName>
<D_NM104_MemberFirstName>JOHN &amp; JACK</D_NM104_MemberFirstName>
Note the quotes do not need to be escaped because they are not between quotes.
However < and > do get escaped as well as the ampersand because they would break the the xml if written as their literal text

I believe this demonstrates that there isn't a reason to use the security element to perform any escaping when converting from edi into xml as that behavior already happens in the .NET xml libraries which you are using to build the xml document.

Tim Meindle
Sep 30, 2016 at 9:50 PM
This has been resolved in version 6.7.5