Answered by:
How to prevent xml escape sequence entities with Linq?

Question
-
HI,
I have generated some xml using the linq method, however, some of the elements are generated using the > and < entities. I'd like those to be translated.
The way I created the xml was by feeding in a string like:
XElement xElm1=new XElement.Parse("<div id="123"> abc </div>");
XElement xElm2=new XElement("Stuff", new XElement(xElm1));
But what comes out is: <div id="123"> abc </div>
I know I can do a replace string, but shouldn't the XElement spit out the properly translated entities??
Thanks!
--PhBMonday, October 13, 2008 12:41 AM
Answers
-
Hi PhrankBooth,
Instead of passing the string values into the XElement constructor, pass the strings into XElement.Parse() and pass that result into the constructor. Here is an example:1 XElement aElem = new XElement("HTML", 2 new XElement("HEAD", 3 new XElement("Style", 4 new XAttribute("type", "text/css"), _Style), 5 new XElement("Script", 6 new XAttribute("type", "text/javascript"), _Script)), 7 new XElement("BODY", XElement.Parse(_Body1), XElement.Parse(_Body2)));
Sincerely,
Kevin Babcock
Kevin Babcock http://blogs.telerik.com/kevinbabcock- Marked as answer by Figo Fei Friday, October 17, 2008 9:40 AM
Friday, October 17, 2008 7:38 AM
All replies
-
I'm not sure why you are having trouble. I was able to use the following code to get the result you are looking for:
1 var xml = "<div id='123'>abc</div>"; 2 var xElement1 = XElement.Parse(xml, LoadOptions.None); 3 var xElement2 = new XElement("Stuff", new XElement(xElement1));
With this code I ended up with the following XML:1 <Stuff> 2 <div id="123">abc</div> 3 </Stuff>
I hope that helps. If not, maybe you could paste a bit more code so I can better understand where you are having difficulties?
Regards,
Kevin Babcock
Monday, October 13, 2008 4:03 AM -
thanks for your reply. Sorry I was going from memory and the actual code is this:
private const string _Style = ".hidden { display: none; }" +
This is what aElem contains, note the entities in the Body:
".unhidden { display: block; }";
private const string _Script = "function unhide(divID) {" +
"var item = document.getElementById(divID);" +
"if (item) {item.className=(item.className=='hidden')?'unhidden':'hidden';}}";
private const string _Body1 = "<div id=\"col1\"><ul></ul></div>";
private const string _Body2 = "<div id=\"col2\"></div>";
XElement aElem = new XElement("HTML",
new XElement("HEAD",
new XElement("Style", new XAttribute("type", "text/css"), _Style),
new XElement("Script", new XAttribute("type", "text/javascript"), _Script)),
new XElement("BODY", _Body1, _Body2));
I expect those to be translated but they aren't. What is wrong here?<HTML>
<HEAD>
<Style type="text/css">.hidden { display: none; }.unhidden { display: block; }</Style>
<Script type="text/javascript">function unhide(divID) {var item = document.getElementById(divID);if (item) {item.className=(item.className=='hidden')?'unhidden':'hidden';}}</Script>
</HEAD>
<BODY><div id="col1"><ul></ul></div><div id="col2"></div></BODY>
</HTML>
--PhBMonday, October 13, 2008 1:55 PM -
No one has any ideas about this?
--PhBWednesday, October 15, 2008 4:27 PM -
Hi PhrankBooth,
Instead of passing the string values into the XElement constructor, pass the strings into XElement.Parse() and pass that result into the constructor. Here is an example:1 XElement aElem = new XElement("HTML", 2 new XElement("HEAD", 3 new XElement("Style", 4 new XAttribute("type", "text/css"), _Style), 5 new XElement("Script", 6 new XAttribute("type", "text/javascript"), _Script)), 7 new XElement("BODY", XElement.Parse(_Body1), XElement.Parse(_Body2)));
Sincerely,
Kevin Babcock
Kevin Babcock http://blogs.telerik.com/kevinbabcock- Marked as answer by Figo Fei Friday, October 17, 2008 9:40 AM
Friday, October 17, 2008 7:38 AM