C# 从c中读取XML#
我正在尝试从c应用程序读取xml文件。到目前为止一点运气都没有。这是XML文件C# 从c中读取XML#,c#,xml,xml-parsing,C#,Xml,Xml Parsing,我正在尝试从c应用程序读取xml文件。到目前为止一点运气都没有。这是XML文件 <?xml version="1.0" encoding="utf-8"?> <ExportJobs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <JobList> <Job Id="555555">
<?xml version="1.0" encoding="utf-8"?>
<ExportJobs xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<JobList>
<Job Id="555555">
<Comments></Comments>
<DueDate>2017-11-17</DueDate>
<FormattedDueDate>17-Nov-2017 12:00</FormattedDueDate>
<TargetDueDate>2017-11-17</TargetDueDate>
<ServiceTypeId>3</ServiceTypeId>
<ServiceType>Service</ServiceType>
<TenantName>Miss Ash</TenantName>
<Uprn>testUpr</Uprn>
<HouseName></HouseName>
</Job>
<Job Id="666666">
<Comments></Comments>
<DueDate>2018-03-15</DueDate>
<FormattedDueDate>15-Mar-2018 12:00</FormattedDueDate>
<TargetDueDate>2018-03-15</TargetDueDate>
<ServiceTypeId>3</ServiceTypeId>
<ServiceType>Service</ServiceType>
<TenantName>Mr Howard</TenantName>
<Uprn>testUpr2</Uprn>
</Job>
</JobList>
</ExportJobs>
我真的很感谢你的帮助。谢谢XmlSerializer是您的朋友:
using System;
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
public class ExportJobs
{
public List<Job> JobList { get; } = new List<Job>();
}
public class Job
{
[XmlAttribute]
public int Id { get; set; }
public string Comments { get; set; }
public DateTime DueDate { get; set; }
public string FormattedDueDate { get; set; }
public DateTime TargetDueDate{ get; set; }
public int ServiceTypeId { get; set; }
public string ServiceType { get; set; }
public string TenantName { get; set; }
public string Uprn { get; set; }
public string HouseName { get; set; }
}
static class P
{
static void Main()
{
var ser = new XmlSerializer(typeof(ExportJobs));
ExportJobs jobs;
using (var sr = new StringReader(xml))
{
jobs = (ExportJobs) ser.Deserialize(sr);
}
foreach(var job in jobs.JobList)
{
Console.WriteLine($"{job.Id} / {job.Uprn}: {job.DueDate}");
}
}
const string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
<ExportJobs xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
<JobList>
<Job Id=""555555"">
<Comments></Comments>
<DueDate>2017-11-17</DueDate>
<FormattedDueDate>17-Nov-2017 12:00</FormattedDueDate>
<TargetDueDate>2017-11-17</TargetDueDate>
<ServiceTypeId>3</ServiceTypeId>
<ServiceType>Service</ServiceType>
<TenantName>Miss Ash</TenantName>
<Uprn>testUpr</Uprn>
<HouseName></HouseName>
</Job>
<Job Id=""666666"">
<Comments></Comments>
<DueDate>2018-03-15</DueDate>
<FormattedDueDate>15-Mar-2018 12:00</FormattedDueDate>
<TargetDueDate>2018-03-15</TargetDueDate>
<ServiceTypeId>3</ServiceTypeId>
<ServiceType>Service</ServiceType>
<TenantName>Mr Howard</TenantName>
<Uprn>testUpr2</Uprn>
</Job>
</JobList>
</ExportJobs>";
}
使用系统;
使用System.Collections.Generic;
使用System.IO;
使用System.Xml.Serialization;
公共类出口工作
{
公共列表作业列表{get;}=new List();
}
公开课工作
{
[XmlAttribute]
公共int Id{get;set;}
公共字符串注释{get;set;}
公共日期时间DueDate{get;set;}
公共字符串FormattedDueDate{get;set;}
公共日期时间TargetDueDate{get;set;}
public int ServiceTypeId{get;set;}
公共字符串服务类型{get;set;}
公共字符串租户名称{get;set;}
公共字符串Uprn{get;set;}
公共字符串HouseName{get;set;}
}
静态P类
{
静态void Main()
{
var ser=新的XmlSerializer(typeof(ExportJobs));
出口工作;
使用(var sr=newstringreader(xml))
{
作业=(导出作业)序列反序列化(sr);
}
foreach(作业中的var作业。作业列表)
{
WriteLine($“{job.Id}/{job.Uprn}:{job.DueDate}”);
}
}
常量字符串xml=@“
2017-11-17
2017年11月17日12:00
2017-11-17
3.
服务
阿什小姐
testUpr
2018-03-15
2018年3月15日12:00
2018-03-15
3.
服务
霍华德先生
testUpr2
";
}
您正在访问根元素的ChildNodes
,它只包含作业
元素,按顺序不包含属性Id
和Uprn
通常的做法是使用XPath
查询,如下所示:
foreach (XmlNode node in xmlDoc.DocumentElement.SelectNodes("Jobs/Job"))
{
costCode = node.Attributes["Id"].InnerText;
uprn = node.SelectSingleNode("Uprn").InnerText;
}
请注意,
Uprn
是节点,而不是节点属性。我认为解决问题的最佳方法是XDocument类
XDocument xDoc = XDocument.Load(@"D:\1.xml");
foreach(var node in xDoc.Descendants("Job"))
{
id = node.Attribute("Id");
foreach(var subnode in node.Descendants("Uprn"))
{
uprn = subnode.Value;
}
//or like that. but check it for null before
uprn = node.Descendants("Uprn")?.First().Value
}
下面是经过测试的代码。您需要名称空间。请参阅下面使用xml linq的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication67
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement exportJobs = doc.Root;
XNamespace ns = exportJobs.GetDefaultNamespace();
var results = exportJobs.Descendants(ns + "Job").Select(x => new {
id = (string)x.Attribute(ns + "Id"),
comment = (string)x.Element(ns + "Comments"),
dueDate = (DateTime)x.Element(ns + "DueDate"),
formattedDueDate = (DateTime)x.Element(ns + "FormattedDueDate"),
targetDueDate = (DateTime)x.Element(ns + "TargetDueDate"),
serviceTypeId = (int)x.Element(ns + "ServiceTypeId"),
serviceType = (string)x.Element(ns + "ServiceType"),
tenantName = (string)x.Element(ns + "TenantName"),
uprn = (string)x.Element(ns + "Uprn"),
houseName = (string)x.Element(ns + "HouseName")
}).ToList();
}
}
}
Uprn
是一个元素,而不是一个属性;然而,XmlSerializer
是您的朋友……然而,Marc Gravell♦'的答案更适用:)顺便说一句:在Visual Studio中有一个“编辑”=>“粘贴特殊”=>“过去的XML作为类”菜单选项,但如果您使用它,您将看到为什么我通常不。。。(将生成的代码与上面答案中的代码进行比较)非常感谢。我来测试一下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication67
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
XElement exportJobs = doc.Root;
XNamespace ns = exportJobs.GetDefaultNamespace();
var results = exportJobs.Descendants(ns + "Job").Select(x => new {
id = (string)x.Attribute(ns + "Id"),
comment = (string)x.Element(ns + "Comments"),
dueDate = (DateTime)x.Element(ns + "DueDate"),
formattedDueDate = (DateTime)x.Element(ns + "FormattedDueDate"),
targetDueDate = (DateTime)x.Element(ns + "TargetDueDate"),
serviceTypeId = (int)x.Element(ns + "ServiceTypeId"),
serviceType = (string)x.Element(ns + "ServiceType"),
tenantName = (string)x.Element(ns + "TenantName"),
uprn = (string)x.Element(ns + "Uprn"),
houseName = (string)x.Element(ns + "HouseName")
}).ToList();
}
}
}