Re: XML Deserialization Newbie Question

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Try adding a call to writer.Flush() just before you reset the stream
position to 0

--
Jared Parsons [MSFT]
jaredpar@xxxxxxxxxxxxxxxxxxxx
http://blogs.msdn.com/jaredpar
"This posting is provided "AS IS" with no warranties, and confers no rights"
"Bill Henning" <please @ nospamforactiprosoftware.com> wrote in message
news:el%23CZZzSFHA.3184@xxxxxxxxxxxxxxxxxxxxxxx
> Hi there.... I'm trying to figure out how to do simple XML serialization
> however whenever I try to deserialize from XML, I get a System Error
> ("System.Xml.XmlException: The root element is missing.").
>
> Could someone try running this sample code to tell me what I'm doing
> wrong? I'm just serializing an int and trying to call CanDeserialize on it
> and that's where the exception occurs. Thanks so much in advance!
>
>
> // ----------------------------------------------------
> string xml;
> int c = 100;
> {
> System.Xml.Serialization.XmlSerializer serializer = new
> System.Xml.Serialization.XmlSerializer(c.GetType());
> System.IO.MemoryStream stream = new System.IO.MemoryStream();
> System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stream,
> null);
> serializer.Serialize(writer, c);
> stream.Position = 0;
> xml = new System.IO.StreamReader(stream).ReadToEnd();
> stream.Close();
> }
> Console.WriteLine(xml);
> {
> System.Xml.Serialization.XmlSerializer serializer = new
> System.Xml.Serialization.XmlSerializer(c.GetType());
> System.IO.MemoryStream stream = new System.IO.MemoryStream();
> System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(stream,
> null);
> writer.WriteRaw(xml);
> stream.Position = 0;
> System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(stream);
> Console.WriteLine(serializer.CanDeserialize(reader));
> }
> return;
> // ----------------------------------------------------
>
>


.



Relevant Pages

  • Re: overriding webservice serializer?
    ... > webservice uses with a custom serializer. ... > compliant serializer and override the .net "default" webservice xml ... very carefully in the sample which stream is used for what in the request, ...
    (microsoft.public.dotnet.framework.webservices)
  • Re: optional nillable values
    ... empty element into something the Xml Serializer would accept. ... > Nullable Type in XML webservice... ... > public bool ElementNameSpecified; ...
    (microsoft.public.dotnet.framework.webservices)
  • OuputFormat and XMLSerializers alternative?
    ... I am making a sample web service program using WSDL and SOAP under JDK ... I used a source below included in XML and JAVA; ... new XMLSerializer(System.out, formatter); ... serializer = new XMLSerializer; ...
    (comp.lang.java.programmer)
  • Serializing / Deserializing XML using System.Xml.Serialization
    ... serializer for that. ... be a string, which would contain an XML text. ... public class Child1 ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: An error in XML document - identity constraint is not declared
    ... DataSet object constraints should be serializable. ... "When you serialize a DataSet object, schema information is important for ... Does it mean that .net xml serializer has a bug? ... > cannot be serialized by the .net xml serializer. ...
    (microsoft.public.dotnet.framework.webservices.enhancements)