C# 用C语言从XML文件中提取数据

C# 用C语言从XML文件中提取数据,c#,asp.net,xml,visual-studio,C#,Asp.net,Xml,Visual Studio,使用vs2010,我的/bin文件夹中有一些XML文件。我想从这个xml中提取一些数据属性和元素值。使用C最好的方法是什么 我需要使用XMLTextReader还是创建一个xmlDocument更好…我很困惑…您可以使用LINQ to XML? 要么是System.Xml要么是System.Linq.Xml 我建议您使用Linq2Xml: 这是一个很好的指南:这里是一个查询XML文件的简单示例 public class Job { public int Id { get; set; }

使用vs2010,我的/bin文件夹中有一些XML文件。我想从这个xml中提取一些数据属性和元素值。使用C最好的方法是什么


我需要使用XMLTextReader还是创建一个xmlDocument更好…我很困惑…

您可以使用LINQ to XML?

要么是System.Xml要么是System.Linq.Xml

我建议您使用Linq2Xml:


这是一个很好的指南:

这里是一个查询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文件填充作业对象

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();
    }

}

我建议你退房。下面是一个小示例,介绍如何使用ek_ny XML文档将XML序列化到对象或从对象序列化/反序列化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文档有很多属性,那么就必须创建一个非常大的类


祝你好运

有一个很好的方法可以做到这一点。你可以用 DataTable.ReadXmlstring文件名 它很容易处理一切。根据问题的不同,它可能有用也可能无效。 注意:这种方法无法从xml文件中获取模式,您应该自己向表中添加列