XML格式 序列化帮助

已加入
2018年9月26日
留言内容
1
编程经验
1-3
您好

我有一大堆xsd.exe从一堆xsd模式文件中生成的代码。这些类可以很好地编译成.dll,并且我可以在代码中使用这些类。

当我尝试使用XmlSerializer(typeof(AuthenticationRequest))时,出现运行时错误'System.NullReferenceException:'对象引用未设置为对象的实例。'
如果创建测试类,则可以使其正常工作,但不能使xsd.exe生成的任何类正常工作。它们都在typeof()调用时失败

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生成的代码如下**
    [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; }
 
最佳 底部