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 "
elementFormDefa ult="qualified"
xmlns="http://tempuri.org/NewsArticle.xsd "
xmlns:mstns="ht tp://tempuri.org/NewsArticle.xsd "
xmlns:xs="http://www.w3.org/2001/XMLSchema"
>
<xs:element name="NewsArtic le">
<xs:complexType >
<xs:sequence>
<xs:element name="Body" type="xs:string " />
</xs:sequence>
<xs:attribute name="ID" type="xs:positi veInteger" />
<xs:attribute name="Title" type="xs:string " />
<xs:attribute name="Date" type="xs:dateTi me" />
</xs:complexType>
</xs:element>
</xs:schema>
using System.Xml.Seri alization;
[System.Serializ ableAttribute()]
[System.Xml.Seri alization.XmlRo otAttribute(IsN ullable=true)]
public partial class NewsArticle {
private string bodyField;
private string idField;
private string titleField;
private System.DateTime dateField;
private bool dateFieldSpecif ied;
public string Body {
get {
return this.bodyField;
}
set {
this.bodyField = value;
}
}
[System.Xml.Seri alization.XmlAt tributeAttribut e(DataType="pos itiveInteger")]
public string ID {
get {
return this.idField;
}
set {
this.idField = value;
}
}
[System.Xml.Seri alization.XmlAt tributeAttribut e()]
public string Title {
get {
return this.titleField ;
}
set {
this.titleField = value;
}
}
[System.Xml.Seri alization.XmlAt tributeAttribut e(DataType="Dat eTime")]
public System.DateTime Date {
get {
return this.dateField;
}
set {
this.dateField = value;
}
}
[System.Xml.Seri alization.XmlIg noreAttribute()]
public bool DateSpecified {
get {
return this.dateFieldS pecified;
}
set {
this.dateFieldS pecified = value;
}
}
}
"Martin Honnen" <ma*******@yaho o.dewrote in message
news:eu******** ******@TK2MSFTN GP03.phx.gbl...
Andy B wrote:
>I have the following code in a default.aspx web form page_load event.
There seems to be a problem with line 5 (NewsArticle.Da te = line).
//create a news article
NewsArticle NewsArticle = new NewsArticle();
NewsArticle.Bo dy = "This is a test news article...";
NewsArticle.Ti tle = "Testing XML";
NewsArticle.Da te = Convert.ToDateT ime("4/3/2008");
NewsArticle. ID = "1";
//serialize the NewsArticle into an xml document
XmlDocument NewsArticleXml = new XmlDocument();
XmlSerialize r Serializer = new XmlSerializer(N ewsArticle.GetT ype());
StringBuilde r SB = new StringBuilder() ;
StringWriter Writer = new StringWriter(SB );
Serializer.Ser ialize(Writer, NewsArticle);
NewsArticleXml .LoadXml(SB.ToS tring());
Response.Clear ();
Response.Conte ntType = "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="htt p://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?
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/