创建指定的xml文档三
2008-12-07 21:52
176 查看
要生成的xml是:
<?xml version="1.0" encoding="utf-16"?>
<学校 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<学生 姓名="李四" 学号="0443111121">
<分数 语文="90" 数学="89" 英语="85" />
</学生>
<学生 姓名="李四" 学号="0443111121">
<分数 语文="90" 数学="89" 英语="85" />
</学生>
</学校>
先序列化xml文件
[XmlRoot("学校")]
public class School
{
List<Student> stu = new List<Student>();
[XmlElement(ElementName = "学生")]
public List<Student> Stu
{
get { return stu; }
set { stu = value; }
}
}
public class Student
{
private string name;
[XmlAttribute(AttributeName="姓名")]
public string Name
{
get { return name; }
set { name = value; }
}
private string stuID;
[XmlAttribute(AttributeName="学号")]
public string StuID
{
get { return stuID; }
set { stuID = value; }
}
private Score score = new Score();
[XmlElement(ElementName = "分数")]
public Score Score
{
get{return score;}
set{score=value;}
}
}
public class Score
{
private string chinese;
[XmlAttribute(AttributeName = "语文")]
public string Chinese
{
get { return chinese; }
set { chinese = value; }
}
private string math;
[XmlAttribute(AttributeName = "数学")]
public string Math
{
get { return math; }
set { math = value; }
}
private string english;
[XmlAttribute(AttributeName = "英语")]
public string English
{
get { return english; }
set { english = value; }
}
}
School s = new School();
Student stu = new Student();
stu.Name = "张三";
stu.StuID = "0443111241";
Score sco = new Score();
sco.Chinese = "95";
sco.English = "85";
sco.Math = "88";
stu.Score = sco;
s.Stu.Add(stu);
stu.Name = "李四";
stu.StuID = "0443111121";
sco.Chinese = "90";
sco.English = "85";
sco.Math = "89";
stu.Score = sco;
s.Stu.Add(stu);
XmlSerializer mySerializer = new XmlSerializer(typeof(School));
StringBuilder sb =new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);
mySerializer.Serialize(xw, s);
xw.Close();
richTextBox1.Text = sb.ToString();
<?xml version="1.0" encoding="utf-16"?>
<学校 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<学生 姓名="李四" 学号="0443111121">
<分数 语文="90" 数学="89" 英语="85" />
</学生>
<学生 姓名="李四" 学号="0443111121">
<分数 语文="90" 数学="89" 英语="85" />
</学生>
</学校>
先序列化xml文件
[XmlRoot("学校")]
public class School
{
List<Student> stu = new List<Student>();
[XmlElement(ElementName = "学生")]
public List<Student> Stu
{
get { return stu; }
set { stu = value; }
}
}
public class Student
{
private string name;
[XmlAttribute(AttributeName="姓名")]
public string Name
{
get { return name; }
set { name = value; }
}
private string stuID;
[XmlAttribute(AttributeName="学号")]
public string StuID
{
get { return stuID; }
set { stuID = value; }
}
private Score score = new Score();
[XmlElement(ElementName = "分数")]
public Score Score
{
get{return score;}
set{score=value;}
}
}
public class Score
{
private string chinese;
[XmlAttribute(AttributeName = "语文")]
public string Chinese
{
get { return chinese; }
set { chinese = value; }
}
private string math;
[XmlAttribute(AttributeName = "数学")]
public string Math
{
get { return math; }
set { math = value; }
}
private string english;
[XmlAttribute(AttributeName = "英语")]
public string English
{
get { return english; }
set { english = value; }
}
}
School s = new School();
Student stu = new Student();
stu.Name = "张三";
stu.StuID = "0443111241";
Score sco = new Score();
sco.Chinese = "95";
sco.English = "85";
sco.Math = "88";
stu.Score = sco;
s.Stu.Add(stu);
stu.Name = "李四";
stu.StuID = "0443111121";
sco.Chinese = "90";
sco.English = "85";
sco.Math = "89";
stu.Score = sco;
s.Stu.Add(stu);
XmlSerializer mySerializer = new XmlSerializer(typeof(School));
StringBuilder sb =new StringBuilder();
XmlWriter xw = XmlWriter.Create(sb);
mySerializer.Serialize(xw, s);
xw.Close();
richTextBox1.Text = sb.ToString();
相关文章推荐
- 创建指定的xml文档一
- 创建指定的xml文档二
- C# 创建简单XML文档
- Java的XML创建、解析文档
- OpenXml SDK 2.0 创建Word文档——最简化的版本
- asp.net使用XML创建Excel文档
- 用JAVA创建XML文档,为java API文档添加搜索功能
- OpenXml To Excel之创建文档2
- C#来创建和读取XML文档
- NSIS ---使用nsDialogs创建自定义页面,并获取输入到控件中的内容保存到一个XML文档中
- 创建一个book.xml文档,PHP通过DOM方式解析获得每本书的作者和书名
- JAVA 基于dom4j创建XML文档
- XMLHelper类 源码(XML文档帮助类,静态方法,实现对XML文档的创建,及节点和属性的增、删、改、查)
- 使用dom4j创建XML文档
- C#来创建和读取XML文档(一)
- WebService生成XML文档时出错。不应是类型XXXX。使用XmlInclude或SoapInclude属性静态指定非已知的类型。
- .NET 通过代码创建XML文档
- JDOM入门实例:读取与创建xml文档
- Java四种读取和创建XML文档的例子
- JDOM入门实例:读取与创建xml文档