Link to home
Create AccountLog in
Avatar of Michael Lam
Michael Lam

asked on

using XmlSerializer getting "The ReadContentAsString method is not supported on node type None" error

i have an xml string that i can deserialize into a customer class object.  but when i try to serialize the object back to a string:

public static String SerializeObject(Object pObject)
        {
            String XmlizedString = null;

            try
            {
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    XmlSerializer xs = new XmlSerializer(typeof(T));
                    XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);

                    xs.Serialize(xmlTextWriter, pObject);
                    XmlTextReader xmlTextReader = new XmlTextReader(memoryStream);

                    //object o = xmlTextReader.ReadContentAsObject();
                    //XmlizedString = o.ToString();
                    XmlizedString = xmlTextReader.ReadContentAsString(); //xmlTextWriter. UTF8ByteArrayToString(memoryStream.ToArray());
                }
               
            }
            catch (Exception e)
            {
                ...
            }

            return XmlizedString;
        }

i keep getting the above error.  i tried ReadContentAsString, ReadContentAs, ReadContentAsObject, but nothing works. now the custom class has properties that i may not include in my xml string, but i don't think it should be an issue.  thanks.
string ftpsXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Adapter>";
            ftpsXML += "<AdapterType>ftps</AdapterType>";
            ftpsXML += "<Url>xxxxxx</Url>";
            ftpsXML += "<User>";
            ftpsXML += userName + "</User><Password>";
            ftpsXML += passWord + "</Password><NumberRetries>2</NumberRetries>";
            ftpsXML += "<Delay>3000</Delay><RemoteDirectory>Subdir1</RemoteDirectory>";
            ftpsXML += "<SourceUrl>xxxxxx</SourceUrl>";
            ftpsXML += "<SourceDirectory>xxxxxx</SourceDirectory>";
            ftpsXML += "<FileNameRegExPattern>" + @"^[\w]*_[\w]{8}-[\w]{4}-[\w]{4}-[\w]{4}-[\w]{12}\.[\w]+$" +
                "</FileNameRegExPattern>";
            ftpsXML += "<EncryptedFileExtension>pgp</EncryptedFileExtension><FtpFormat>FTP</FtpFormat>";
            ftpsXML += "<EncryptionAlgorithmEntityID>1</EncryptionAlgorithmEntityID><EnableSsl>true</EnableSsl>";
            ftpsXML += "</Adapter>";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account