Help with XML Serialization

Matthew Crocker

New member
Joined
Sep 26, 2018
Messages
1
Programming Experience
1-3
Hello

I have a large chunk of code generated by xsd.exe from a bunch of xsd schema files. The classes compile into a .dll just fine and I can use the classes in my code just fine.

When I try to use XmlSerializer(typeof(AuthenticationRequest)) I get a run-time error 'System.NullReferenceException: 'Object reference not set to an instance of an object.'
If I create a test class I can get it to work but I can't get any class generated by xsd.exe to work. They all fail at the typeof() call

public class Program
{
    static public int Main()
    {
        OCIMessage message = new OCIMessage();
        AuthenticationRequest command = new AuthenticationRequest();


        command.userId = "matthew";
        message.command[0] = command;
        
        XmlSerializer ser = new XmlSerializer(typeof(OCIMessage));


        String XmlizedString = null;
        
        MemoryStream memoryStream = new MemoryStream();


        XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);


        ser.Serialize(xmlTextWriter, command);


        memoryStream = (MemoryStream)xmlTextWriter.BaseStream;


        UTF8Encoding encoding = new UTF8Encoding();


        XmlizedString = encoding.GetString(memoryStream.ToArray());


        XmlizedString = XmlizedString.Substring(1);


        Console.Out.WriteLine(XmlizedString);


        String input = Console.In.ReadLine();
        return 0;
    }
}

** XSD Generated code below **
    [DebuggerStepThrough]
    [DesignerCategory("code")]
    [GeneratedCode("xsd", "4.6.1055.0")]
    [XmlRoot("BroadsoftDocument", Namespace = "C", IsNullable = false)]
    [XmlType(Namespace = "C")]
    public class OCIMessage
    {
        public OCIMessage();


        [XmlChoiceIdentifier("ItemElementName")]
        [XmlElement("phoneNumber", typeof(string), Form = XmlSchemaForm.Unqualified, DataType = "token")]
        [XmlElement("sessionId", typeof(string), Form = XmlSchemaForm.Unqualified, DataType = "normalizedString")]
        [XmlElement("userId", typeof(string), Form = XmlSchemaForm.Unqualified, DataType = "token")]
        public string Item { get; set; }
        [XmlIgnore]
        public ItemChoiceType ItemElementName { get; set; }
        [XmlElement("command", Form = XmlSchemaForm.Unqualified)]
        public OCICommand[] command { get; set; }
        [XmlAttribute]
        public OCIMessageProtocol protocol { get; set; }
 
Back
Top Bottom