avatar
o*a
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();
}
}
}
avatar
k*i
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
: {

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。