0

I have tried to use the Json.Convert method to get my content into xml however my html content is encoded (by json.net)

Note: I dont have access to the system.web dll

<br />
<br />

My class:

public class TemplateContent
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string VersionNumber { get; set; }
    public string CoverPage { get; set; }
    public string Body { get; set; }
}

I want the prop CoverPage - to be in straight up html without encodings

My code: first convert to json... (content is the above object)

        var json = JsonConvert.SerializeObject(content);
        return json;

then transform to xml

        var myXmlNode = JsonConvert.DeserializeXNode(json, "Template");

Can this bit of the code JsonConvert.DeserializeXNode - be forced to decode the content? or leave the content as it is when converting into xml?

<Template>
  <ID>14</ID>
  <Name>name of report</Name>
  <VersionNumber>1.0</VersionNumber>
  <CoverPage>






  &lt;br /&gt;
  &lt;br /&gt;
  &lt;h3 style="text-align: center;"&gt;
    &lt;br class="GENTICS_ephemera" /&gt;
  &lt;/h3&gt;
  &lt;h3 style="text-align: center;"&gt;
    &lt;br class="GENTICS_ephemera" /&gt;
  &lt;/h3&gt;
  &lt;h3 style="text-align: center;"&gt;Property Valuation Report&lt;/h3&gt;     

        </CoverPage>
  <Body>




  &lt;h4 style="text-align: center;"&gt;Property Valuation Report&lt;/h4&gt;
  &lt;p&gt;
  &lt;/p&gt;
  &lt;p&gt;
  &lt;/p&gt;</Body>
</Template>

enter image description here

3
  • @Harron, your xml is valid and if you parse it with an xml parser(or deserialize to a class) you'll get correct(decoded) values. Commented Feb 6, 2012 at 14:57
  • @L.B - how would I do that? (sorry I am a newbie) Commented Feb 6, 2012 at 14:58
  • @L.B - Yes, my xml is valid so I know half the job is done Commented Feb 6, 2012 at 14:58

2 Answers 2

1

Try

var json = JsonConvert.SerializeObject(content.Replace("&lt;", "<").Replace("&gt;", "<"));
return json;
Sign up to request clarification or add additional context in comments.

2 Comments

dont have access to the system.web - even if i did, json.net is destroying it as I stated in my question so this wont work
Ok, try a string replace. Modified answer to reflect. Also, if that gets more complex, say... needing to decode Entities, you may just want to write a function, or add using System.Web to your class and then use the built-in functions.
0

your xml is valid and if you parse it with an xml parser(or deserialize to a class) you'll get correct(decoded) values. For example,

string xmlx = @"
<Template>
  <ID>14</ID>
  <Name>name of report</Name>
  <VersionNumber>1.0</VersionNumber>
  <CoverPage>
&lt;br /&gt;
&lt;br /&gt;
&lt;h3 style=""text-align: center;""&gt;
&lt;br class=""GENTICS_ephemera"" /&gt;
&lt;/h3&gt;
&lt;h3 style=""text-align: center;""&gt;
&lt;br class=""GENTICS_ephemera"" /&gt;
&lt;/h3&gt;
&lt;h3 style=""text-align: center;""&gt;Property Valuation Report&lt;/h3&gt;  
  </CoverPage>
</Template>";

XElement xelem = XElement.Load(new StringReader(xmlx));
string xmlvalue = xelem.Descendants("CoverPage").First().Value;

will return

<br />
<br />
<h3 style="text-align: center;">
<br class="GENTICS_ephemera" />
</h3>
<h3 style="text-align: center;">
<br class="GENTICS_ephemera" />
</h3>
<h3 style="text-align: center;">Property Valuation Report</h3>  

So answer to or leave the content as it is when converting into xml? is Yes

9 Comments

Strange... just tried it and it does not work... &lt;br /&gt; will not work
I have ran your tests and it does seem to work when you actually iterate over the tag, however I am using xslt to transform the document from the above ancoded into xsl fo... my xslt document is not picking this up, should I open another question?
@Haroon, I tested above code before I posted. It does work. Just copy-paste it to some project and try.
Sorry- I meant to say when iterating over the document whilst doing my xslt transform the tags are not being picked up - because I check if something is li - then do something.
Also: when doing a console.writeline on your example you get the encoded values
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.