Create XML document from object list
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using System.Reflection;
using System.Xml.Linq;
class Employee {
int _id;
int _idRole;
string _lastName;
string _firstName;
public int ID {
get { return _id; }
set { _id = value; }
}
public int IDRole {
get { return _idRole; }
set { _idRole = value; }
}
public string LastName {
get { return _lastName; }
set { _lastName = value; }
}
public string FirstName {
get { return _firstName; }
set { _firstName = value; }
}
}
class Program {
static void Main(string[] args) {
List<Employee> people = new List<Employee> {
new Employee { ID = 1, IDRole = 1, LastName = "A", FirstName = "B"},
new Employee { ID = 2, IDRole = 2, LastName = "G", FirstName = "T"}
};
XElement xml = new XElement("people",
from p in people
select new XElement("person",
new XElement("id", p.ID),
new XElement("firstname", p.FirstName),
new XElement("lastname", p.LastName),
new XElement("idrole", p.IDRole)));
Console.WriteLine(xml);
}
根据数据生成xml文档
private void btnSave_Click(object sender, RoutedEventArgs e)
{
this.SelectedPracticeInfo = new PracticeInfo();
this.SelectedPracticeInfo.LessonDescription = "•Excuse me! 对?不?起e.这a是?常£用?于ú表í示?道à歉?";
this.SelectedPracticeInfo.LessonID = "12";
this.SelectedPracticeInfo.LessonName = "1 Excuse me! 对?不?起e!?";
this.SelectedPracticeInfo.ListExercise = new List<ExerciseInfo>();
ExerciseInfo info = new ExerciseInfo();
info.Title = "七?、¢根ù据Y中D文?补1全?句?子ó";
info.Type = "Translate";
info.ListQuestionAnswer = new List<QuestionsAnswersInfo>();
QuestionsAnswersInfo infoQA = new QuestionsAnswersInfo();
infoQA.Alternative = "A:这a是?你?的?上?衣?吗e??";
infoQA.Answer = "Is this your coat?";
infoQA.Question2 = "Is _ your _?";
info.ListQuestionAnswer.Add(infoQA);
this.SelectedPracticeInfo.ListExercise.Add(info);
XElement xml = new XElement("Practice", new XElement("LessonID", this.SelectedPracticeInfo.LessonID),
new XElement("LessonDescription", this.SelectedPracticeInfo.LessonDescription),
new XElement("LessonName", this.SelectedPracticeInfo.LessonName),
new XElement("Exercises",
from p in this.SelectedPracticeInfo.ListExercise
select new XElement("Exercise",
new XElement("Type",p.Type),
new XElement("Title",p.Title),
new XElement("QuestionsAnswers",
from item in p.ListQuestionAnswer
select new XElement("QuestionAnswer",
new XElement("Alternative",item.Alternative),
new XElement("Answer",item.Answer),
new XElement("Question",item.Question),
new XElement("Question2",item.Question2)
)
)
)
)
);
MessageBox.Show(xml.ToString());
}
类
public class PracticeInfo
{
public string LessonID { get; set; }
public string LessonDescription { get; set; }
public string LessonName { get; set; }
public List<ExerciseInfo> ListExercise { get; set; }
}
public class ExerciseInfo
{
public string Type { get; set; }
public string Title { get; set; }
public List<QuestionsAnswersInfo> ListQuestionAnswer { get; set; }
}
public class QuestionsAnswersInfo
{
public string Question { get; set; }
public string Answer { get; set; }
/// <summary>
/// /// </summary>
public string Alternative { get; set; }
public string Question2 { get; set; }
/// <summary>
///
/// </summary>
public string Tape { get; set; }
}
生成的xml文件
<Practice>
<LessonID>12</LessonID>
<LessonDescription>•Excuse me! 对?不?起e.这a是?常£用?于ú表í示?道à歉?</LessonDescription>
<LessonName>1 Excuse me! 对?不?起e!?</LessonName>
<Exercises>
<Exercise>
<Type>Translate</Type>
<Title>七?、¢根ù据Y中D文?补1全?句?子ó</Title>
<QuestionsAnswers>
<QuestionAnswer>
<Alternative>A:这a是?你?的?上?衣?吗e??</Alternative>
<Answer>Is this your coat?</Answer>
<Question />
<Question2>Is _ your _?</Question2>
</QuestionAnswer>
</QuestionsAnswers>
</Exercise>
</Exercises>
</Practice>