1

I would like to put settings to my XmlWriter. I've an issue when I serialize my object with XmlWriter. My object is simple object with get/set.

When I call the serialize function I've this error :

Error generating XML document

Full StackTrace :

Inner Exception : 

System.InvalidOperationException {"WriteStartDocument can not be called 
on writers created with ConformanceLevel.Fragment."} System.InvalidOperationException



Erreur lors de la génération du document XML.
----------------------------------------------------  
 à System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces, String encodingStyle, String id)

 à System.Xml.Serialization.XmlSerializer.Serialize(XmlWriter xmlWriter, Object o, XmlSerializerNamespaces namespaces)

 à ...\Classes\MyClasse.vb:ligne 350

Public Function ObjectToXML() As String
    Try
        Dim xmlSerializer As New XmlSerializer(Me.GetType, AttrOverrides)
        Dim ns As New XmlSerializerNamespaces()
        ns.Add("", "")
        Dim settings As New XmlWriterSettings()
        settings.ConformanceLevel = ConformanceLevel.Fragment
        settings.OmitXmlDeclaration = True

        Dim writer As XmlWriter = XmlWriter.Create(New MemoryStream(), settings)
        xmlSerializer.Serialize(writer, Me, ns)
        writer.Close()

        Return writer.ToString
    Catch ex As Exception
        Error()
    End Try
End Function

My serialization work when I remove settings from my XmlWriter.

How to use settings with XmlWriter ?

QuentinV
  • 139
  • 1
  • 12
  • 1
    Is there no more detailed error message available? Can you debug the code and post the stack trace? – Martin Honnen Jan 10 '17 at 11:05
  • I've posted the full stacktrace. There are not lot of infos. – QuentinV Jan 10 '17 at 11:09
  • Can you look (with the debugger) for an InnerException? – H H Jan 10 '17 at 11:11
  • i've added the InnerException. – QuentinV Jan 10 '17 at 11:15
  • So "WriteStartDocument can not be called on writers created with ConformanceLevel.Fragment." tells you that the line `settings.ConformanceLevel = ConformanceLevel.Fragment` is the culprit. Why do you think you need that? I think `XmlSerializer` is always generating a well-formed document. – Martin Honnen Jan 10 '17 at 11:16
  • I need this for remove this in my XML : . I've found the solution here : http://stackoverflow.com/questions/1980187/removing-version-from-xml-file but it doesn't work for me – QuentinV Jan 10 '17 at 11:21
  • 1
    That is the XML declaration and using `settings.OmitXmlDeclaration = True` suffices to omit it. – Martin Honnen Jan 10 '17 at 11:22
  • Good, it work. But I also thought that the root could be removed with ConformanceLevel.Fragment. Finally, I need a malformed XML without root and declaration. Now I've not the declaration but the root is always present. Can I remove the root easily ? – QuentinV Jan 10 '17 at 11:28
  • 1
    "Finally, I need a malformed XML without root and declaration.": really? And whoever has to consume that stuff then shows up here complaining that the XML parser used can't parse that "XML". But well, if you want to omit the root element I would consider using `Dim resultDoc as New XmlDocument()` and then `xmlSerializer.Serializer(resultDoc.CreateNavigator().AppendChild(), Me)` and finally to return `resultDoc.DocumentElement.InnerXml`. I don't think XmlSerializer has a way doing it solely with the Serialize method and an XmlWriter. – Martin Honnen Jan 10 '17 at 12:22
  • "without root and declaration" - You won't be able to convince the XmlSerializer to omit the root. Your only option would seem to be to remove it later and to get your malformed xml from the serialized data. Easier with an XElement, if the data is not too big. – H H Jan 10 '17 at 13:08
  • *Finally, I need a malformed XML without root*. Neither `XmlWriter` or `XmlSerializer` support creating "rootless" invalid XML out of the box. To do this you will need something like `RootlessDataSetXmlWriter` from [this answer](http://stackoverflow.com/a/9232306/3744182). – dbc Feb 03 '17 at 17:42

0 Answers0