6

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">
  <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">
  <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">
  <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?

csharpnoob
  • 505
  • 1
  • 9
  • 24
  • 1
    Why do you care? The two are identical semantically. – John Saunders Oct 01 '09 at 21:12
  • 1
    Becourse the tool consuming it, doesn't like it. On first on it shows the HTML Text. On 2nd it shows "Hello World" bold. I can't change the consuming tool. – csharpnoob Oct 01 '09 at 21:31
  • Then there's something else wrong with the tool, since you don't have `Hello World` so it shouldn't be bold. – John Saunders Oct 01 '09 at 21:35
  • John "If the tool accepts the CDATA version but not the escaped version, then the tool has a fatal bug that needs to be fixed. The two are sematically identical XML. Perhaps the tool does not understand XML? Beyond that, I don't understand your question. Also, C# is a programming language. It does not have any serializers at all. You appear to be referring to .NET serializers, and you happen to be using them from the C# programming language." Right, but why is there anyway a CDATA Tag on XML Standard? If there is another way of creating XML Files in C# from Classes.. open for any Solution. – csharpnoob Oct 01 '09 at 21:36
  • ups...

    should be , fixed in question. happend on creating the example, problem is still the same. Consuming Tool can't be changed it productive and company made it is gone!

    – csharpnoob Oct 01 '09 at 21:39
  • I wasn't involved in the design of XML. :-) However, remember that XML stands for "eXtensible Markup Language". The primary use case was someone having plain text, and adding "markup" characters to it. If a person is creating XML from text, then being able to put a CDATA around existing text makes sense. CDATA was not created because programs need it. – John Saunders Oct 01 '09 at 21:46

3 Answers3

7

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
  • 286,951
  • 70
  • 623
  • 758
0

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
  • 160,644
  • 26
  • 247
  • 397
  • 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 Oct 01 '09 at 21:52
  • 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 Oct 01 '09 at 22:12
  • I'm using already the JsonExSerializer, which does the job fine. I also need XML. Object > XML – csharpnoob Oct 01 '09 at 22:45
  • 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 Oct 01 '09 at 23:47
0

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
  • 7,094
  • 4
  • 37
  • 43