From: Ele on
Hello,

I'm encountering a strange issue when deserializing a piece of XML. If I
have the following XML and try to deserialize it, everything works:

<page>
<format>Letter</format>
<margin>1</margin>
</page>

If I have the following XML, everything works:

<page>
<format></format>
<margin>1</margin>
</page>

BUT if instead I have the following XML, the "margin" element is always 0:

<page>
<format />
<margin>1</margin>
</page>

Why? Is there a known issue with this format?
Thanks!


From: Peter Duniho on
Ele wrote:
> Hello,
>
> I'm encountering a strange issue when deserializing a piece of XML. If I
> have the following XML and try to deserialize it, everything works:
> [...]
> BUT if instead I have the following XML, the "margin" element is always 0:
>
> <page>
> <format />
> <margin>1</margin>
> </page>
>
> Why? Is there a known issue with this format?

The XML is fine, at least based on the other XML examples you provided.
So there's something wrong with your deserialization code.

But since you didn't post any code, never mind a concise-but-complete
code example that reliably demonstrates the problem, it's not possible
to identify the problem, never mind suggest a solution.

Pete
From: Ele on
Hi,

I have replicated the issue in a small .cs file. Here it is.

You can download it from here:
http://www.megaupload.com/?d=7BQVL3CD

Or from here (it's the same file):
http://rapidshare.com/files/351297504/Example.zip.html

Thank you! :-)


From: Peter Duniho on
Ele wrote:
> Hi,
>
> I have replicated the issue in a small .cs file. Here it is. [links snipped]

Post it to this newsgroup, please. Not only do I (and perhaps others)
not have interest in visiting random web sites to download files, part
of the point of having a community forum such as this is that the
content is available to others at a later date, even much later than
your file will still be available on those web sites.

By including your code in a newsgroup post proper, you ensure that
future readers will still have the necessary context to benefit from
whatever discussion takes place now.

Thanks,
Pete
From: Ele on
Hello,

Yes, I understand your point. I did not post it to NG because I didn't want
to add too much code in my message, making it difficult to read. However,
here's the code.
As always, thank you! :-)



using System;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication1
{
[XmlRoot(Namespace = "", ElementName = "element")]
public class CDataElement
{
private System.Xml.XmlNode nodeValue;

[XmlElement("distance")]
public double Distance { get; set; }

[XmlElement("value")]
public System.Xml.XmlNode Value
{
get { return nodeValue; }
set { nodeValue = value; }
}

public void SetValue(string Val)
{
nodeValue = GetCData(Val);
}

public string GetValue()
{
return ((nodeValue == null) ? null : nodeValue.Value);
}

private XmlCDataSection GetCData(string value)
{
XmlDataDocument doc = new XmlDataDocument();
return doc.CreateCDataSection(value);
}
}

class Example
{
static void Main(string[] args)
{
/* ELEMENT IS NOT CORRECT WITH THE FOLLOWING XML */
string strXml = "<element>" +
"<value />" +
"<distance>2</distance>" +
"</element>";
CDataElement el =
(CDataElement)DeserializeObject(typeof(CDataElement), strXml);

/* ELEMENT IS CORRECT */
strXml = "<element>" +
"<value></value>" +
"<distance>2</distance>" +
"</element>";
el = (CDataElement)DeserializeObject(typeof(CDataElement),
strXml);
}

public static object DeserializeObject(Type Type, string XML)
{
System.IO.StringReader stringreader = new
System.IO.StringReader(XML);
XmlSerializer serializer = new XmlSerializer(Type);
object obj = serializer.Deserialize(stringreader);
serializer = null;
stringreader.Dispose();
stringreader = null;

return obj;
}

public static string SerializeObject<T>(T Source)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
System.IO.Stream stream = new System.IO.MemoryStream();
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
serializer.Serialize(stream, Source, ns);

stream.Seek(0, System.IO.SeekOrigin.Begin);
System.IO.StreamReader reader = new
System.IO.StreamReader(stream);
string contents = reader.ReadToEnd();

reader.Dispose();
reader = null;
stream.Dispose();
stream = null;
serializer = null;

return contents;
}
}
}