Missing required element in serialized XML
|
| Missing required element in serialized XML | I am using .NET framework 1.1 SP1, .NET framework SDK 1.1 SP1, with hotfix 82202, Visual studio .NET 2003 with hotfix 823639. I have generated a proxy class using wsdl.exe from a schema that has an xsd:date element called XYZ_IncDate.
If I change the xsd:date element to be optional, then wsdl.exe generates two attributes for the date field in the proxy class:
[System.Xml.Serialization.XmlAttributeAttribute(DataType="date")] public System.DateTime XYZ_IncDate; /// [System.Xml.Serialization.XmlIgnoreAttribute()] public bool XYZ_IncDateSpecified; If the element is required, then it only generates one attribute. [System.Xml.Serialization.XmlAttributeAttribute(DataType="date")] The problem I am having is, when the element is required, it is not serialized in the XML, and my web service request fails because this element really is required, and it's not there. I can verify that that value is being set in the proxy class, but it does not appear in the XML. When the element is optional, I can force the attribute to appear in the XML by setting the XYZ_IncDateSpecified to true, but I don't have that option in the case where the attribute is required, because no XYZ_IncDateSpecified attribute is created in the proxy.
It seems to me that the behavior in XML serialization for the case where the element is required is exactly the opposite of what it should be. I can understand not needing the XYZ_IncDateSpecified flag, because if it's required it should always be serialized in the XML. But the opposite is happening! It is never serialized in the XML, and you are not even given the option to try to force it to serialize the value via the XYZ_IncDateSpecified, as you can when it is optional. Is this a known bug in XML serialization, and does anyone know what can be done to fix/work around this without requiring manual editing of schema and proxy classes?
The trouble is, we need to have a solution that works for people who are generating their XML schemas on the fly based on their data types - it is up to their discretion whether certain attributes of their data are required or not. Once they have created their datatype, we generate a schema which obeys their wishes as far as whether certain elements are required or not, and we create a wsdl referencing their schema for their data type. It's not just a matter of tweaking one schema or one proxy class. We want to be able to have users generate their schema and wsdl, generate the proxy, and run, without having to do manual edits on the proxy class.
-- Cindy
| | Yes, it is dependent on both the fact that the xsd:date type is mapped to a ValueType and that it is required instead of optional. When I say in my schema file that an element is "required", that logically means, regardless of what type that element is, that it is must appear in the XML document, and that without it the XML document is invalid. Therefore, when the XML is serialized, the element and its value should always be included. On the contrary, the behavior I see is that when the XML is serialized, no value *ever* appears for this element, even though it is clear that the proxy class has a valid value for the DateTime type set. I believe that this is a clear bug in the XML serialization code. -- Cindy
"Steven Cheng[MSFT]" wrote:
> Hi Cindy, > > Thank you for posting. > From your description, you're using .net framework to generate the > webservice client proxy for some webservice, and you're manually > constructing the WSDL document/xml schema for the service. However, you're > encountering some problem when generate the proxy class's property for a > date type attribute with use=required, correct? > Based on the code snippet and xml element definition you provided, I think > the .net generation code on the date type attribute is not only depend on > the "use" attribute in the xml schema, but also depend on the type in .net > framework which is used to represent the property/field of that xml > attribute. For Date type, .net framework will use the System.DateTime type > to represent it. However, since DateTime is a value type class(not > reference type), so any property/field of this type will always has a valid > instance on the variable, the property or field won't be null. That's why > when we set the xml attribute in schema as use="required", the generated > .net class field won't make any explicit change on it. And at runtime, you > can found even we don't assign any value to the Date property, there will > exists an instance on it. And only if the scheme type for the attribute is > set to use="optional" will the .net framework generate another field > (XXXSpecified) to indicate whether the value is null or not..... > This is the rule .net framework generate the property according to > property/attribute in webservice's WSDL doc. > Regards, > Steven Cheng > Microsoft Online Community Support > ================================================== > When responding to posts, please "Reply to Group" via your newsreader so > that others may learn and benefit from your issue. > This posting is provided "AS IS" with no warranties, and confers no rights. > Get Secure! www.microsoft.com/security > (This posting is provided "AS IS", with no warranties, and confers no > rights
| |  |
|
|
|
|
 | Members Area | |
|
 | Last Web Marketing |
|
|
|
|
 | Last Programming Tips |
|
|
|
|
 | Last News |
|
|
|
|
|
|