【发布时间】:2012-01-10 13:08:33
【问题描述】:
我的 /bin 文件夹中有一些 XML 文件(使用 vs2010)。我想从这个 xml 中提取一些数据(属性、元素值)。使用 C# 执行此操作的最佳方法是什么?
我需要使用 XMLTextReader 还是创建一个 xmlDocument 会更好...我很困惑...
【问题讨论】:
标签: c# asp.net xml visual-studio
我的 /bin 文件夹中有一些 XML 文件(使用 vs2010)。我想从这个 xml 中提取一些数据(属性、元素值)。使用 C# 执行此操作的最佳方法是什么?
我需要使用 XMLTextReader 还是创建一个 xmlDocument 会更好...我很困惑...
【问题讨论】:
标签: c# asp.net xml visual-studio
您可以使用 LINQ to XML 吗? http://msdn.microsoft.com/en-us/library/bb387098.aspx
【讨论】:
System.Xml 或者 System.Linq.Xml
我会推荐 Linq2Xml:http://msdn.microsoft.com/en-us/library/bb387098.aspx
这是一个很好的指南:http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx
【讨论】:
这是一个查询 XML 文件的简单示例
public class Job
{
public int Id { get; set; }
public int CompanyId { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public int HoursPerWeek { get; set; }
}
XML 文件片段:
<?xml version="1.0" encoding="utf-8"?>
<Jobs>
<Job>
<Id>1</Id>
<CompanyId>2</CompanyId>
<Description>Must be willing to relocate to Nebraska.</Description>
<HoursPerWeek>90</HoursPerWeek>
<JobStatus>1</JobStatus>
<JobType>3</JobType>
<Location>NY</Location>
<Title>Application Engineer for Gooooogle Plus</Title>
</Job>
<Job>
从 XML 文件填充 Job 对象的类
public class XMLJobsDAL
{
XDocument JobDoc
{
get { return XDocument.Load(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/Job.xml")); }
}
public List<Job> GetJobs()
{
var results = from job in JobDoc.Descendants("Job")
select new Job
{
Id = (int)job.Element("Id"),
CompanyId = (int)job.Element("CompanyId"),
Description = (string)job.Element("Description"),
HoursPerWeek = (int)job.Element("HoursPerWeek"),
Title = (string) job.Element("Title")
}
return results.ToList();
}
public Job GetJob(int id)
{
var result = from job in JobDoc.Descendants("Job")
where (int)job.Element("Id") == id
select new Job
{
Id = (int)job.Element("Id"),
CompanyId = (int)job.Element("CompanyId"),
Description = (string)job.Element("Description"),
HoursPerWeek = (int)job.Element("HoursPerWeek"),
Title = (string) job.Element("Title")
}
return result.SingleOrDefault();
}
}
【讨论】:
我建议您查看Serialization。下面是一个关于如何使用 ek_ny XML 文档对对象进行序列化/反序列化的小示例:
using System.IO;
using System.Xml.Serialization;
[Serializable]
public class Job
{
public Job() { }
public int ID { get; set; }
public int CompanyID { get; set; }
public string Description { get; set; }
public int HoursPerWeek { get; set; }
public int JobStatus { get; set; }
public int JobType { get; set; }
public string Location { get; set; }
public string Title { get; set; }
public void SerializeToXML(string path)
{
//creates a file
FileStream fs = new FileStream(path, FileMode.Create);
//now we create the serializer
XmlSerializer xs = new XmlSerializer(typeof(Job));
//serialize it to the file
xs.Serialize(fs, this);
//close the file
fs.Close();
}
public static Job DeserializeToJob(string path) {
//open file
FileStream fs = new FileStream(path, FileMode.Open);
//create deserializer
XmlSerializer xs = new XmlSerializer(typeof(Job));
//Deserialize
Job job = (Job)xs.Deserialize(fs);
//close the file
fs.Close();
//return the deserialized job
return job;
}
}
实施:
class Program
{
static void Main(string[] args)
{
Job j = new Job();
j.CompanyID = 2;
j.ID = 1;
j.Description = "Must be willing to relocate to Nebraska.";
j.HoursPerWeek = 90;
j.JobStatus = 1;
j.JobType = 3;
j.Location = "NY";
j.Title = "Application Engineer for Gooooogle Plus";
//Saving the object serialized.
j.SerializeToXML("MyJob.xml");
//deserialize the saved job into a new instance
Job j2 = Job.DeserializeToJob("MyJob.xml");
}
}
这样,您将在 XML 之间来回获取对象。这对我来说是最有效的。我在这里看到的唯一缺点是,如果您的 XML 文档有很多属性,您将不得不创建一个非常大的类。
http://support.microsoft.com/kb/815813
http://www.switchonthecode.com/tutorials/csharp-tutorial-xml-serialization
祝你好运!
【讨论】:
有一种很好的方法可以做到这一点。您可以使用
DataTable.ReadXml(string fileName)
它可以轻松处理所有事情。根据问题,它可能有用也可能没用。
请注意,此方法无法从 xml 文件中获取架构,您应该自己将列添加到表中。
【讨论】: