Serialize XML is broken?

A

Andy B

I have the following code in a default.aspx web form page_load event. There
seems to be a problem with line 5 (NewsArticle.Date = line).

//create a news article

NewsArticle NewsArticle = new NewsArticle();

NewsArticle.Body = "This is a test news article...";

NewsArticle.Title = "Testing XML";

NewsArticle.Date = Convert.ToDateTime("4/3/2008");

NewsArticle.ID = "1";

//serialize the NewsArticle into an xml document

XmlDocument NewsArticleXml = new XmlDocument();

XmlSerializer Serializer = new XmlSerializer(NewsArticle.GetType());

StringBuilder SB = new StringBuilder();

StringWriter Writer = new StringWriter(SB);

Serializer.Serialize(Writer, NewsArticle);

NewsArticleXml.LoadXml(SB.ToString());

Response.Clear();

Response.ContentType = "text/xml";

NewsArticleXml.Save(Response.Output);



The result of the last line above in a web browser is this: Any idea where
the date attribute went? The NewsArticle object above was converted with xsd
from a schema to a class.

<?xml version="1.0" encoding="utf-8" ?>
- <NewsArticle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="1" Title="Testing XML">
<Body>This is a test news article...</Body>
</NewsArticle>
 
M

Martin Honnen

Andy said:
I have the following code in a default.aspx web form page_load event. There
seems to be a problem with line 5 (NewsArticle.Date = line).

//create a news article

NewsArticle NewsArticle = new NewsArticle();

NewsArticle.Body = "This is a test news article...";

NewsArticle.Title = "Testing XML";

NewsArticle.Date = Convert.ToDateTime("4/3/2008");

NewsArticle.ID = "1";

//serialize the NewsArticle into an xml document

XmlDocument NewsArticleXml = new XmlDocument();

XmlSerializer Serializer = new XmlSerializer(NewsArticle.GetType());

StringBuilder SB = new StringBuilder();

StringWriter Writer = new StringWriter(SB);

Serializer.Serialize(Writer, NewsArticle);

NewsArticleXml.LoadXml(SB.ToString());

Response.Clear();

Response.ContentType = "text/xml";

NewsArticleXml.Save(Response.Output);

I don't understand why you need the XmlDocument, you should simply let
the Serialize method write to Response.Output, no need for StringWriter
and XmlDocument.

The result of the last line above in a web browser is this: Any idea where
the date attribute went? The NewsArticle object above was converted with xsd
from a schema to a class.

<?xml version="1.0" encoding="utf-8" ?>
- <NewsArticle xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" ID="1" Title="Testing XML">
<Body>This is a test news article...</Body>
</NewsArticle>

Can you post the schema and the .NET code for NewsArticle?
 
F

Family Tree Mike

You must be doing some custom serialization to get some properties as
attributes (id, title) and some as elements (body). The problem likely is in
your NewsArticle class.
 
A

Andy B

Hi...

The XmlDocument was supposed to be created to make it easier to insert into
a database as an xml field. Is there any better way of doing this than what
was listed? I suppose I could just serialize to a string and insert that
into the database. Then load it as xml when it is needed. Anyways, here is
the schema and .net class for NewsArticle:

<?xml version="1.0" encoding="utf-8"?>

<xs:schema id="NewsArticle"

targetNamespace="http://tempuri.org/NewsArticle.xsd"

elementFormDefault="qualified"

xmlns="http://tempuri.org/NewsArticle.xsd"

xmlns:mstns="http://tempuri.org/NewsArticle.xsd"

xmlns:xs="http://www.w3.org/2001/XMLSchema"

<xs:element name="NewsArticle">

<xs:complexType>

<xs:sequence>

<xs:element name="Body" type="xs:string" />

</xs:sequence>

<xs:attribute name="ID" type="xs:positiveInteger" />

<xs:attribute name="Title" type="xs:string" />

<xs:attribute name="Date" type="xs:dateTime" />

</xs:complexType>

</xs:element>

</xs:schema>

using System.Xml.Serialization;

[System.SerializableAttribute()]

[System.Xml.Serialization.XmlRootAttribute(IsNullable=true)]

public partial class NewsArticle {


private string bodyField;

private string idField;

private string titleField;

private System.DateTime dateField;

private bool dateFieldSpecified;


public string Body {

get {

return this.bodyField;

}

set {

this.bodyField = value;

}

}


[System.Xml.Serialization.XmlAttributeAttribute(DataType="positiveInteger")]

public string ID {

get {

return this.idField;

}

set {

this.idField = value;

}

}


[System.Xml.Serialization.XmlAttributeAttribute()]

public string Title {

get {

return this.titleField;

}

set {

this.titleField = value;

}

}


[System.Xml.Serialization.XmlAttributeAttribute(DataType="DateTime")]

public System.DateTime Date {

get {

return this.dateField;

}

set {

this.dateField = value;

}

}


[System.Xml.Serialization.XmlIgnoreAttribute()]

public bool DateSpecified {

get {

return this.dateFieldSpecified;

}

set {

this.dateFieldSpecified = value;

}

}

}
 
M

Martin Honnen

Andy said:
[System.SerializableAttribute()]

[System.Xml.Serialization.XmlRootAttribute(IsNullable=true)]

public partial class NewsArticle {


private string bodyField;

private string idField;

private string titleField;

private System.DateTime dateField;

private bool dateFieldSpecified;


public string Body {

get {

return this.bodyField;

}

set {

this.bodyField = value;

}

}


[System.Xml.Serialization.XmlAttributeAttribute(DataType="positiveInteger")]

public string ID {

get {

return this.idField;

}

set {

this.idField = value;

}

}


[System.Xml.Serialization.XmlAttributeAttribute()]

public string Title {

get {

return this.titleField;

}

set {

this.titleField = value;

}

}


[System.Xml.Serialization.XmlAttributeAttribute(DataType="DateTime")]

public System.DateTime Date {

get {

return this.dateField;

}

set {

this.dateField = value;

}

}


[System.Xml.Serialization.XmlIgnoreAttribute()]

public bool DateSpecified {

get {

return this.dateFieldSpecified;

}

set {

this.dateFieldSpecified = value;

}

With that class definition you need to set

NewsArticle.DateSpecified = true;

to have the Date field show up.
 
C

Chris Shepherd

Family said:
You must be doing some custom serialization to get some properties as
attributes (id, title) and some as elements (body). The problem likely is in
your NewsArticle class.

Applying attributes to the properties isn't really custom serialization is it?

You can slap [XmlElement("property1")] before a property, and have it serialize
as an actual element, and [XmlAttribute("property2")] to have it serialize as an
attribute.

I'm not being critical here, just curious if that's considered custom
serialization. To my mind the point at which you need to write your own actual
serialization logic, rather than let .NET handle it, is the point at which it's
now custom serialization.

Chris.
 
A

Andy B

Sounds like this might be more like custom xml document formatting more than
custom serialization....


Chris Shepherd said:
Family said:
You must be doing some custom serialization to get some properties as
attributes (id, title) and some as elements (body). The problem likely
is in your NewsArticle class.

Applying attributes to the properties isn't really custom serialization is
it?

You can slap [XmlElement("property1")] before a property, and have it
serialize as an actual element, and [XmlAttribute("property2")] to have it
serialize as an attribute.

I'm not being critical here, just curious if that's considered custom
serialization. To my mind the point at which you need to write your own
actual serialization logic, rather than let .NET handle it, is the point
at which it's now custom serialization.

Chris.
 
A

Andy B

Ok setting NewsArticle.DateSpecified = true; did the job... tnx for the
help...


Martin Honnen said:
Andy said:
[System.SerializableAttribute()]

[System.Xml.Serialization.XmlRootAttribute(IsNullable=true)]

public partial class NewsArticle {


private string bodyField;

private string idField;

private string titleField;

private System.DateTime dateField;

private bool dateFieldSpecified;


public string Body {

get {

return this.bodyField;

}

set {

this.bodyField = value;

}

}


[System.Xml.Serialization.XmlAttributeAttribute(DataType="positiveInteger")]

public string ID {

get {

return this.idField;

}

set {

this.idField = value;

}

}


[System.Xml.Serialization.XmlAttributeAttribute()]

public string Title {

get {

return this.titleField;

}

set {

this.titleField = value;

}

}


[System.Xml.Serialization.XmlAttributeAttribute(DataType="DateTime")]

public System.DateTime Date {

get {

return this.dateField;

}

set {

this.dateField = value;

}

}


[System.Xml.Serialization.XmlIgnoreAttribute()]

public bool DateSpecified {

get {

return this.dateFieldSpecified;

}

set {

this.dateFieldSpecified = value;

}

With that class definition you need to set

NewsArticle.DateSpecified = true;

to have the Date field show up.
 
F

Family Tree Mike

Yes, that is what I should have said. What I meant was custom relative to
what the class would be without any attributes on the properties. That could
be done in a variety of ways, not having seen the code at that point.

Andy B said:
Sounds like this might be more like custom xml document formatting more than
custom serialization....


Chris Shepherd said:
Family said:
You must be doing some custom serialization to get some properties as
attributes (id, title) and some as elements (body). The problem likely
is in your NewsArticle class.

Applying attributes to the properties isn't really custom serialization is
it?

You can slap [XmlElement("property1")] before a property, and have it
serialize as an actual element, and [XmlAttribute("property2")] to have it
serialize as an attribute.

I'm not being critical here, just curious if that's considered custom
serialization. To my mind the point at which you need to write your own
actual serialization logic, rather than let .NET handle it, is the point
at which it's now custom serialization.

Chris.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top