I am well aware of _why_ the exception occurs. Now I need a way to work
around it. How can I provide a "hint" to the XmlSerializer that if the root
David B. Bitton
> David,
>
> The code you posted works fine for me with this code:
>
> TransferManifest mfst = new TransferManifest();
> string xml = TransferManifest.WriteXml( mfst );
> mfst = TransferManifest.ReadXml( xml );
>
> but I removed the three array type properties
>
> /// <remarks/>
> [System.Xml.Serialization.XmlElementAttribute("filelist")]
> public FileList[] FileLists;
>
> /// <remarks/>
> [System.Xml.Serialization.XmlElementAttribute("folderlist")]
> public FolderList[] FolderLists;
>
> /// <remarks/>
> [System.Xml.Serialization.XmlElementAttribute("uploadinfo")]
> public UploadInfo[] UploadInfos;
>
> because I didn't have the class definitions.
>
> The root element
>
> <transfermanifest>
>
> you posted would not deserialize because it's missing the XML namespace
that
> you declared with the XmlRoot and XmlType attributes. The root element
needs
> a namespace declaration like this:
>
> <transfermanifest xmlns=
http://codenoevil.com/TransferManifest.xsd> >
> which your code produces.
>
> You could either post more code, or follow the instructions at [0] on how
to
> debug deserialization.
>
> --
> HTH
> Christoph Schittko [MVP]
> Software Architect, .NET Mentor
>
> "David B. Bitton" <david@codenoevil.com> wrote in message
> news:%23q6PRPHKEHA.2680@TK2MSFTNGP11.phx.gbl...
> > Chris,
> > Look inline below:
> >
> > --
> > --
> >
> > David B. Bitton
> > david@codenoevil.com
> >
www.codenoevil.com > >
> > Code Made Fresh DailyT
> > "Christoph Schittko [MVP]" <christophdotnetINVALID@austin.rr.com> wrote
in
> > message news:eN4XNSCKEHA.952@TK2MSFTNGP12.phx.gbl...
> > > David,
> > >
> > > Can you post:
> > >
> > > * The serialization class
> >
> >
>
[System.Xml.Serialization.XmlTypeAttribute(Namespace=TransferManifest.DEFAUL
> > T_NAMESPACE)]
> >
>
[System.Xml.Serialization.XmlRootAttribute(Namespace=TransferManifest.DEFAUL
> > T_NAMESPACE, IsNullable=false, ElementName="transfermanifest")]
> > public class TransferManifest {
> >
> > public const string DEFAULT_NAMESPACE =
> > "
http://codenoevil.com/TransferManifest.xsd"; > >
> > /// <remarks/>
> > [System.Xml.Serialization.XmlElementAttribute("filelist")]
> > public FileList[] FileLists;
> >
> > /// <remarks/>
> > [System.Xml.Serialization.XmlElementAttribute("folderlist")]
> > public FolderList[] FolderLists;
> >
> > /// <remarks/>
> > [System.Xml.Serialization.XmlElementAttribute("uploadinfo")]
> > public UploadInfo[] UploadInfos;
> >
> > #region XML (de)serialization code
> > public static TransferManifest ReadXml(string xml)
> > {
> > XmlSerializer xmlSerializer =
> > new XmlSerializer(typeof(TransferManifest));
> >
> > xmlSerializer.UnknownNode+= new
> > XmlNodeEventHandler(XmlSerializer_UnknownNode);
> > xmlSerializer.UnknownAttribute+= new
> > XmlAttributeEventHandler(XmlSerializer_UnknownAttribute);
> >
> > try
> > {
> > TransferManifest transferManifest =
> > (TransferManifest)xmlSerializer.Deserialize(
> > new StringReader(xml)
> > );
> >
> > return transferManifest;
> > }
> > catch(Exception ex)
> > {
> > throw new ApplicationException("TransferManifest failed to
deserialize:
> "
> > + ex.Message, ex);
> > }
> > }
> >
> > public static string WriteXml(TransferManifest transferManifest)
> > {
> > XmlSerializer xmlSerializer =
> > new XmlSerializer(typeof(TransferManifest), DEFAULT_NAMESPACE);
> >
> > StringWriter stringWriter = new StringWriter();
> >
> > xmlSerializer.Serialize(stringWriter, transferManifest);
> >
> > return stringWriter.ToString();
> > }
> >
> > private static void XmlSerializer_UnknownNode
> > (object sender, XmlNodeEventArgs e)
> > {
> > Debug.WriteLine("Unknown Node:" + e.Name + "\t" + e.Text);
> > }
> >
> > private static void XmlSerializer_UnknownAttribute
> > (object sender, XmlAttributeEventArgs e)
> > {
> > System.Xml.XmlAttribute attr = e.Attr;
> > Debug.WriteLine("Unknown attribute " +
> > attr.Name + "='" + attr.Value + "'");
> > }
> > #endregion
> > }
> >
> >
> > > * the tag of the root element, including all namespace definitions
> >
> > <transfermanifest>
> >
> > > * the line of code where you instantiate the XmlSerializer
> >
> > Look inside the WriteXml() method above.
> >
> > >
> > > --
> > > Christoph Schittko [MVP]
> > > Software Architect, .NET Mentor
> > >
> > > "David B. Bitton" <david@codenoevil.com> wrote in message
> > > news:%23Je9uj6JEHA.2244@tk2msftngp13.phx.gbl...
> > > > I am having a problem deserializing XML when the root node is
missing
> a
> > > > namespace declaration. My Type has an XmlTypeAttribute with a
> namespace
> > > > defined. If I attempt to deserialize the XML, I get the dreaded
> > > >
> > > > <elementname xmlns=''> was not expected
> > > >
> > > > exception. If I comment out the XmlTypeAttribute, it works just
fine.
> > > Just
> > > > so you know, when I instantiate an instance of an XmlSerializer, I
> pass
> > a
> > > > default namespace to the ctor. Unfortunately that doesn't seem to
> work.
> > > >
> > > > So, how can I get the XmlSerializer to accept XML that is missing
> > > namespace
> > > > declarations?
> > > >
> > > > --
> > > > --
> > > >
> > > > David B. Bitton
> > > > david@codenoevil.com
> > > >
www.codenoevil.com > > > >
> > > > Code Made Fresh DailyT
> > > >
> > > >
> > >
> > >
> >
> >
>
>