2

I'm currently trying to serialize a class into XML to be posted to php web service.

Whenever I did the normal serialization using XMLSerializer, XML declaration is always appear in the first line of the XML document (similar as to <?xml ....?>). I tested the XML and unable to get it working because the endpoint does not accept XML declaration and I can't do anything about it. I'm unfamiliar with XML Serialization in C# to be honest.

Therefore, I used XMLWriter to do this as below :-

private string SerializeClassToString(GetRiskReport value)
{
    var emptyNS = new XmlSerializerNamespaces(new[] { XmlQualifiedName.Empty });
    var ser = new XmlSerializer(value.GetType());
    var settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;

    using (var stream = new StringWriter())
    {
        using (var writer = XmlWriter.Create(stream, settings))
        {
            ser.Serialize(writer, value, emptyNS);
            return stream.ToString();
        }
    }
}

Result for the Namespace is

<GetRiskReport FCRA=\"false\" ReturnResultsOnly=\"false\" Monitoring=\"false\">

... and I'm able to omit the XML Declaration, however I'm being introduced with 2 new problem.

I got \r\n for new line and I have escaped double quote such as ReturnResultsOnly=\"false\" Monitoring=\"false\" which is also unable processed by the endpoint.

I would like to ask is that does anyone can give me an idea on how to change the XmlWriterSetting to omit XML Declaration, avoid \r\n and also avoid escaped double quotes \"

Thanks for your advice in advance.

Simon

Simon Loh
  • 325
  • 5
  • 15
  • Could you give an reproducible example of what you mean by "escaped double quote"? When inside an XML string a quote `"` character should be escaped as `"`. When Visual Studio displays strings for you, it "helpfully" escapes quote characters. To see the strings without this effect, try typing `Debug.WriteLine(xml)` in the immediate window. – dbc Jan 16 '15 at 03:19
  • @dbc : I have a namespace such as `` and I'm expecting `` Sorry if I use the wrong term. – Simon Loh Jan 16 '15 at 03:34
  • Can you give a reproducible example of how your `GetRiskReport` was generated? It doesn't have to be the full class, just something that demonstrates the quote escaping. – dbc Jan 16 '15 at 04:15

2 Answers2

2

Try with following settings

settings.NewLineHandling = NewLineHandling.None;
settings.CheckCharacters = false;
Pankaj Kapare
  • 7,486
  • 5
  • 40
  • 56
  • setting.NewLineHandling = NewLineHandling.None works. However escaping double quotes are still a problem :( – Simon Loh Jan 16 '15 at 03:13
  • I think double quote shouldn't be issue here. Since you may be checking value of string in debugger you will see those escape characters. Try writing output string to file and see how it turns out. It should be fine. – Pankaj Kapare Jan 16 '15 at 03:21
  • Sorry if I use the wrong term. I caught the double quote sent to client endpoint is as `\"` and not `"` which the client endpoint throw an exception. – Simon Loh Jan 16 '15 at 03:37
0
private void SerializeClassToString(GetRiskReport value)
{
    var emptyNS = new XmlSerializerNamespaces(new[]{XmlQualifiedName.Empty});
    var ser = new XmlSerializer(value.GetType());
    var settings = new XmlWriterSettings();
    settings.OmitXmlDeclaration = true;

string path = 'your_file_path_here'
if (File.Exists(path)) File.Delete(path);
FileStream stream = File.Create(path);
using (var writer = XmlWriter.Create(stream, settings))
    {
        ser.Serialize(writer, value, emptyNS);
        return;
    }
}

There was no way to avoid ms bug or thier intensional specification about xmlserializing.It's easier and faster to use filestream object.

Teru
  • 1
  • 3