C# 在这里读取xml的方法是什么?

C# 在这里读取xml的方法是什么?,c#,asp.net,xml,C#,Asp.net,Xml,我有以下xml文件:- <?xml version="1.0" encoding="utf-8" ?> <LoanProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Program> <ProgramID>6</ProgramID> <Name>Primary Loan</Name> <InterestRate>0.23<

我有以下xml文件:-

<?xml version="1.0" encoding="utf-8" ?>
<LoanProduct xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Program>
<ProgramID>6</ProgramID>
<Name>Primary Loan</Name>
<InterestRate>0.23</InterestRate>
<StartDate>2018-12-20</StartDate>
<EndDate>2019-03-31</EndDate>
</Program>

<Program>
<ProgramID>6</ProgramID>
<Name>Primary Loan</Name>
<InterestRate>0.25</InterestRate>
<StartDate>2019-04-1</StartDate>
<EndDate>2099-12-31</EndDate>
</Program>
</LoanProduct>
假设我想检索ProgramID=6和EndDate='2099-12-31'
如何实现这一点?

您可以通过使用命名空间
System.Xml.Linq
下的
XDocument
来实现所需的结果

XDocument doc = XDocument.Load(@"Path to your xml file");
var ns = doc.Root.GetDefaultNamespace();

var result = (from program in doc.Descendants(ns + "Program")
              where Convert.ToInt32(program.Element(ns + "ProgramID").Value) == 6 && program.Element(ns + "EndDate").Value == "2099-12-31"
              select program).FirstOrDefault();
输出:(来自调试器)


您可以使用序列化。日期应一致,否则您将收到此代码的错误

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication3
{
    class Program1
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(LoanProduct));

            LoanProduct loanProduct = (LoanProduct)serializer.Deserialize(reader);

        }
    }
    [XmlRoot(ElementName = "LoanProduct", Namespace = "")]
    public class LoanProduct
    {
        [XmlElement("Program", Namespace = "")]
        public List<Program> programs { get; set; }
    }
    [XmlRoot(ElementName = "Program", Namespace = "")]
    public class Program
    {
        public int ProgramID { get; set; }
        public string Name { get; set; }
        public decimal InterestRate { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml;
使用System.Xml.Serialization;
命名空间控制台应用程序3
{
班级计划1
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XmlReader=XmlReader.Create(文件名);
XmlSerializer serializer=新的XmlSerializer(typeof(LoanProduct));
LoanProduct LoanProduct=(LoanProduct)序列化程序。反序列化(读取器);
}
}
[XmlRoot(ElementName=“LoanProduct”,Namespace=”“)]
公共类贷款产品
{
[XmlElement(“程序”,名称空间=”)]
公共列表程序{get;set;}
}
[XmlRoot(ElementName=“Program”,Namespace=”“)]
公共课程
{
公共int程序ID{get;set;}
公共字符串名称{get;set;}
公共十进制利率{get;set;}
公共日期时间起始日期{get;set;}
公共日期时间结束日期{get;set;}
}
}

您在
数据集中是否特别需要它?我通常会将它加载到
XDocument
,这使得直接查询非常简单。在这种情况下,我建议您阅读LINQtoXML教程(例如),看看这是否对您有所帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

namespace ConsoleApplication3
{
    class Program1
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(LoanProduct));

            LoanProduct loanProduct = (LoanProduct)serializer.Deserialize(reader);

        }
    }
    [XmlRoot(ElementName = "LoanProduct", Namespace = "")]
    public class LoanProduct
    {
        [XmlElement("Program", Namespace = "")]
        public List<Program> programs { get; set; }
    }
    [XmlRoot(ElementName = "Program", Namespace = "")]
    public class Program
    {
        public int ProgramID { get; set; }
        public string Name { get; set; }
        public decimal InterestRate { get; set; }
        public DateTime StartDate { get; set; }
        public DateTime EndDate { get; set; }
    }
}