Deserialize Xml to "myObject". Exception: " System.Xml.XmlException: Root Element is missing "  
Author Message
LudHouse





PostPosted: ASMX Web Services and XML Serialization, Deserialize Xml to "myObject". Exception: " System.Xml.XmlException: Root Element is missing " Top

Here is the situation:
I use an objetCompose:

using System.Xml;

using

System.IO;

using

System.Xml.Serialization;

namespace

CYTOMICS.CytoGest.Import.Objects.Compose

{

[

XmlRoot(ElementName="ObjetCompose")]

public class ObjetCompose

{

[

XmlElement(ElementName="tabColonnes")]

public string[] tabColonnes;

public void SetValeur( EnumNomColonnes enumNomColonnes, string valeur )

{

tabColonnes[(

int) enumNomColonnes] = valeur;

}

I serialize it like this:

public

string SerializeCompose()

{

string strXml = null;

XmlSerializer s = new XmlSerializer (typeof(ObjetCompose));

MemoryStream memStream = new MemoryStream ();

s.Serialize(memStream, objetCompose);

strXml = System.Text.

Encoding.UTF8.GetString(memStream.GetBuffer());

strXml = strXml.Replace(

"\0", "").Replace( "\r\n", "");

memStream.Close();

return strXml;

}

I send my xml file to my server and then I deserialize in this methode
public ObjetCompose DeserializeCompose( string xmlCompose )

{

XmlSerializer xmlSerializer = new XmlSerializer (typeof(ObjetCompose));

XmlDocument xmlDocument = new XmlDocument ();

MemoryStream memoryStream = new MemoryStream ();

xmlDocument.LoadXml(xmlCompose);

xmlDocument.Save(memoryStream);

return (ObjetCompose) xmlSerializer.Deserialize(memoryStream); here is the crash System.Xml.XmlException: Root Element is missing

}

I so would like to know how I could do to resolve this


.NET Development18  
 
 
LudHouse





PostPosted: ASMX Web Services and XML Serialization, Deserialize Xml to "myObject". Exception: " System.Xml.XmlException: Root Element is missing " Top

I found the solution i should use a stringReader and not a streamReader...

public ObjetCompose DeserializeCompose( string xmlCompose )

{

XmlSerializer xmlSerializer = new XmlSerializer(typeof(ObjetCompose));

StringReader stringReader = new StringReader(xmlCompose);

return (ObjetCompose) xmlSerializer.Deserialize(stringReader);

}

Regards !