Groups | Blog | Home
all groups > dotnet xml > november 2003 >

dotnet xml : processing instructions in XML



Reggie Burnett
11/13/2003 11:09:47 AM
I have some XML that contains an element like this:
<ProcessingStartedOn xsi:nil=\"true\"/>



This element is of type dateTime. It is failing deserialization. How do I
handle this?

Reggie

Christoph Schittko [MVP]
11/13/2003 2:55:08 PM
How are you deserializing and what are you deserializing into?

--
HTH
Christoph Schittko [MVP, XmlInsider]
Software Architect, .NET Mentor

[quoted text, click to view]

Reggie Burnett
11/13/2003 3:58:36 PM
I create an XmlSerializer giving the type of the object. I then call
deserialize with an XmlReader.
As long as I leave out the xml that contains the processing instruction, it
deserializes just fine. With it, it fails saying there is an error in the
XML document.


"Christoph Schittko [MVP]" <christophdotnetINVALID@austin.rr.com> wrote in
message news:eFTUthiqDHA.2820@TK2MSFTNGP10.phx.gbl...
[quoted text, click to view]

Christoph Schittko [MVP]
11/13/2003 9:51:27 PM
I think I am missing something.

I don't see a processing instruction in the Xml that you posted.

As far as

<ProcessingStartedOn xsi:nil=\"true\"/>

goes: You said that ProcessingStartedOn is of type DateTime, which is a
ValueType and therefore can't by nullable. It can, however, be optional if
that helps you out.

Otherwise you're pretty much stuck. You may have to deserialize
ProcessingStartedOn into a string or an XmlElement and post-process the
deserialized Xml manually


--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

[quoted text, click to view]

Reggie Burnett
11/17/2003 11:01:00 AM
[quoted text, click to view]

ok, so you are saying that xsi:nil="true" is saying that the value is null.
So what I hear you saying is that is invalid xml.
I would need to read this into a string and then "detect" the xsi:nil stuff
manually?


Reggie

Christoph Schittko [MVP]
11/18/2003 10:20:01 PM
Not quite ... it is not invalid XML, but the content of that XML cannot be
mapped to the .NET type system.

The XML snippet says that the value of ProcessingStartedOn should be null.
ValueTypes in the .NET framework, such as the DateTime type, cannot be null
and you mentioned that you are trying to deserialize the ProcessingStartedOn
element to a field of type DateTime.

With reference types you can tell the XmlSerializer to handle the the
xsi:nil="true" attribute correctly, by setting the IsNullable property of
the XmlElement attribute to true, but again, this doesn't work for value
types.

If absolutely have to be able to parse this XML, maybe because you're not in
control of the XML document generation, then you need to somehow work around
this limitation. The easiest way would be to read the ProcessingStartedOn
element into a string property instead of a DateTime field directly. Since
string is a reference type you will be able to specify the IsNullable
property. The property set can then internally set a flag when the element
wasn't null and contained a valid date and set the value to a real DateTime
field. The code could look something like this:

public class foo
{
[XmlIgnore]
public DateTime ProcessingStartedOn;

[XmlIgnore]
public DateTime StartTimeSet = false;

[XmlElement("ProcessingStartedOn")]
public string ProcessingStartedOnString
{
get { return ProcessingStartedOn.ToString(); } // or whatever
ToXXXString you need
set { if( null != value )
{
ProcessingStartedOn = DateTime.Parse( value );
StartTimeSet = true;
}
}
}
}

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

[quoted text, click to view]

Amol Kher [MSFT]
11/24/2003 11:57:13 AM
Need more information about the schema.

-Amol

[quoted text, click to view]

AddThis Social Bookmark Button