o**********a 发帖数: 330 | 1 刚接触 xml,为什么第一段code 可以work。 而第2段code不能正确地创建xml
多谢
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace xmlapp
{
public class Movie
{
public string Title
{ get; set; }
public int Rating
{ get; set; }
public DateTime ReleaseDate
{ get; set; }
}
class Program
{
static void Main(string[] args)
{
Movie movie = new Movie();
movie.Title = "Starship Troopers";
movie.ReleaseDate = DateTime.Parse("11/7/1997");
movie.Rating = (int)6.9f;
SerializeToXML(movie);
}
static public void SerializeToXML(Movie movie)
{
XmlSerializer serializer = new XmlSerializer(typeof(Movie));
TextWriter textWriter = new StreamWriter(@"d:\movie.xml");
serializer.Serialize(textWriter, movie);
textWriter.Close();
}
}
}
///////////////////////////////////
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace xmlapp
{
public class Movie
{
readonly string title;
public string Title
{ get {return title;} }
readonly int rating;
public int Rating
{ get { return rating; } }
readonly DateTime releaseDate;
public DateTime ReleaseDate
{ get {return releaseDate;} }
public Movie(string title, int rating, DateTime releaseDate)
{
this.title = title;
this.rating = rating;
this.releaseDate = releaseDate;
}
public Movie() { }
}
class Program
{
static void Main(string[] args)
{
DateTime rd = DateTime.Parse("11/7/1997");
Movie movie = new Movie("hello", 34, rd);
SerializeToXML(movie);
}
static public void SerializeToXML(Movie movie)
{
XmlSerializer serializer = new XmlSerializer(typeof(Movie));
TextWriter textWriter = new StreamWriter(@"d:\movie.xml");
serializer.Serialize(textWriter, movie);
textWriter.Close();
}
}
} | k****i 发帖数: 1072 | 2 Need to have setter when using xmlserializer. Use DataContractSerializer
instead.
【在 o**********a 的大作中提到】 : 刚接触 xml,为什么第一段code 可以work。 而第2段code不能正确地创建xml : 多谢 : using System; : using System.Collections.Generic; : using System.Linq; : using System.Text; : using System.Xml.Serialization; : using System.IO; : namespace xmlapp : {
|
|