views:

751

answers:

3

Okay this one DID it! Thanks to all of you!

public class Result
{
    public String htmlEscaped
    {
        set;
        get;
    }

    [XmlIgnore]
    public String htmlValue
    { set; get; }

    [XmlElement("htmlValue")]
    public XmlCDataSection htmlValueCData
    {
        get
        {
            XmlDocument _dummyDoc = new XmlDocument();
            return _dummyDoc.CreateCDataSection(htmlValue);
        }
        set { htmlValue = (value != null) ? value.Data : null; }
    }
}

    Result r = new Result();
    r.htmlValue = ("<b>Hello</b>");
    r.htmlEscaped = ("<b>Hello</b>");
    XmlSerializer xml = new XmlSerializer(r.GetType());
    TextWriter file = new StreamWriter(Environment.CurrentDirectory + "\\results\\result.xml", false, System.Text.Encoding.Default);
    xml.Serialize(file, r);
    file.Close();

RESULT:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <htmlEscaped>&lt;b&gt;Hello&lt;/b&gt;</htmlEscaped>
  <htmlValue><![CDATA[<b>Hello</b>]]></htmlValue>
</Result>

As you can see, after CDATA is return type, no more escaped html in XML file on filesystem. The JSON Serialization isn't working anymore, but this can be fixed with a little type extention.


QUESTION WAS:

Maybe someone knows how to make do it...

I have this Class:

public class Result
{
    public String htmlValue
    {
        get;
        set;
    }
}

I use this to serialize it to XML

Result res = new Result();
res.htmlValue = "<p>Hello World</p>";
XmlSerializer s = new XmlSerializer(res.GetType());
TextWriter w = new StreamWriter(Environment.CurrentDirectory + "\\result.xml", false, System.Text.Encoding.Default);
s.Serialize(w, res);
w.Close();

Works fine i get this:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <htmlValue>&lt;b&gt;Hello World&lt;/b&gt;</htmlValue>
</Result>

What can do i have to change to get this:

<?xml version="1.0" encoding="Windows-1252"?>
<Result xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  <htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>
</Result>

I've already searched but I can't find anything. The type of htmlValue have to stay String, because of other Serialisations JSON, etc.

Tricky one... Thanks in advance for suggestions

  • HTML is correct in String within C#. Why decode or encode?
  • XmlSerializer saved the HTML escaped to XML file.
  • Don't use C# for consuming.

Is external tool which accept this:

<htmlValue><![CDATA[<b>Hello World</b>]]></htmlValue>

but not

<htmlValue>&lt;b&gt;Hello World&lt;/b&gt;</htmlValue>

I do the same with JSON Serializer, in file on hard drive the HTML is saved correct. Why and where to use HTTP Utility to prevent that? And how to get <![CDATA[ ]]> around it.

Can you give a code sample? Are there any other Serializer than the C# own one?

I've found this Link .NET XML Serialization of CDATA ATTRIBUTE from Marco André Silva, which does I need to do, but it's different, how to include this without changing Types?

A: 

See "CDATA serialization with XMLSerializer" for the same problem, and for the solution.

BTW, it seems to me that if the vendor no longer exists, it's time to use a different product. Possibly one that understands the XML specifications which have only existed for over a decade.

John Saunders
There was an error reflecting type. [XmlElement("node", typeof(XmlCDataSection))] public string htmlValue { get; set; }Return needs to be String because of JSON Serializer.
csharpnoob
I think you need to post some code. First you're talking about XML Serialization, now JSON serialization. I don't understand what problem you're trying to solve.
John Saunders
I'm using already the JsonExSerializer, which does the job fine. I also need XML. Object > XML
csharpnoob
I don't understand what you're trying to do? That's why I want you to post some code showing what you're trying to do.
John Saunders
A: 

It is my understanding that you need the XML to feed it to some utility. Do you also plan to use it to de-serialize the object?

If not then why do not do it yourself - serialize your object that is? Roundtrip object -> XML -> object is somewhat tricky, but the first part is not.

mfeingold
object > XML is enought
csharpnoob
+2  A: 

Here's a simple trick to do achieve what you want. You just need to serialize a XmlCDataSection property instead of the string property :

(it's almost the same as John's suggestion, but a bit simpler...)

public class Result
{
    [XmlIgnore]
    public String htmlValue
    {
        get;
        set;
    }

    private static XmlDocument _dummyDoc;

    [XmlElement("htmlValue")]
    public XmlCDataSection htmlValueCData
    {
        get { return _dummyDoc.CreateCDataSection(htmlValue); }
        set { htmlValue = (value != null) ? value.Data : null; }
    }
}
Thomas Levesque
My "suggestion" was from a case where the OP needed CDATA as a text value.
John Saunders
comes out empty, only XML Header. Maybe [XmlIgnore] on htmlValue affects [XmlElement("htmlValue")]. Have you tried it?
csharpnoob
After Debugging a bit, this one is working! Thanks mate.
csharpnoob