L
Linus
Hi,
I use XSD to generate my classed based on an XML schema, then
include those classes in my web service code. My web service test
client reads an XML document based on the same schema and tries to
deserialize it into those XSD-generated classes. My problem is that
when I try to populate one of the classes, .NET throws an "Object
reference not set to an instance of an object" error.
Here's my XML, schema, and source code, I'd appreciate it if you
could help. Thanks.
XML:
========================================================
<?xml version="1.0" encoding="UTF-8"?>
<Event xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\Web400\contract\test.xsd">
<Config>
<Ticket>121212</Ticket>
<User>Josh</User>
</Config>
</Event>
Schema:
========================================================
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="Event">
<xs:complexType>
<xs:sequence>
<xs:element name="Config">
<xs:complexType>
<xs:sequence>
<xs:element name="Ticket" type="xs:int"/>
<xs:element name="User" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XSD-generated Classes:
=========================================================
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org",
IsNullable=false)]
public class Event
{
public EventConfig Config;
}
public class EventConfig
{
public int Ticket;
public string User;
}
Web Service:
===================================================
[WebMethod()]
[SoapDocumentMethod(ParameterStyle=SoapParameterStyle.Bare)]
public Event AddEvent(Event ev)
{
ev.Config.User = "KK";
return ev;
}
Test Client:
================================================
private void AddEvent()
{
Event ev = null;
FileStream fs = new FileStream("test.xml", FileMode.Open);
XmlReader reader = new XmlTextReader(fs);
XmlSerializer xs = new XmlSerializer(typeof(Event));
try
{
ev = (Event)xs.Deserialize(reader);
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
ev.Config.User = "Smith"; <<< this is where it bombs
ev = _service.AddEvent(ev);
}
I use XSD to generate my classed based on an XML schema, then
include those classes in my web service code. My web service test
client reads an XML document based on the same schema and tries to
deserialize it into those XSD-generated classes. My problem is that
when I try to populate one of the classes, .NET throws an "Object
reference not set to an instance of an object" error.
Here's my XML, schema, and source code, I'd appreciate it if you
could help. Thanks.
XML:
========================================================
<?xml version="1.0" encoding="UTF-8"?>
<Event xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="C:\Web400\contract\test.xsd">
<Config>
<Ticket>121212</Ticket>
<User>Josh</User>
</Config>
</Event>
Schema:
========================================================
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="Event">
<xs:complexType>
<xs:sequence>
<xs:element name="Config">
<xs:complexType>
<xs:sequence>
<xs:element name="Ticket" type="xs:int"/>
<xs:element name="User" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
XSD-generated Classes:
=========================================================
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://tempuri.org",
IsNullable=false)]
public class Event
{
public EventConfig Config;
}
public class EventConfig
{
public int Ticket;
public string User;
}
Web Service:
===================================================
[WebMethod()]
[SoapDocumentMethod(ParameterStyle=SoapParameterStyle.Bare)]
public Event AddEvent(Event ev)
{
ev.Config.User = "KK";
return ev;
}
Test Client:
================================================
private void AddEvent()
{
Event ev = null;
FileStream fs = new FileStream("test.xml", FileMode.Open);
XmlReader reader = new XmlTextReader(fs);
XmlSerializer xs = new XmlSerializer(typeof(Event));
try
{
ev = (Event)xs.Deserialize(reader);
}
catch (Exception e)
{
System.Console.WriteLine(e.Message);
}
ev.Config.User = "Smith"; <<< this is where it bombs
ev = _service.AddEvent(ev);
}