将XDocument立即节点值linq检索到xml

时间:2018-03-28 23:03:11

标签: c# xml xml-parsing linq-to-xml

我有以下XML数据:

<?xml version="1.0"?>
<response version="1.0">
    <respcode>200</respcode>
    <respmessage>Success</respmessage>
    <datetime>Thu, 18 Jan 2018 06:20:25 +0000</datetime>
    <location>
        <site_code>COH</site_code>
        <phone>781-383-0180</phone>
        <fax>781-383-9093</fax>
        <standardized_operation_hours>
            <Monday>07:30-17:00</Monday>
            <Tuesday>07:30-17:00</Tuesday>
            <Wednesday>07:30-17:00</Wednesday>
            <Thursday>07:30-17:00</Thursday>
            <Friday>07:30-17:00</Friday>
            <Saturday>X</Saturday>
            <Sunday>X</Sunday>
    </standardized_operation_hours>
    <scheduling>YES</scheduling>
    <time_zone>America/New_York</time_zone>
    </location>
</response>

这是我的代码,用于解析和检索子元素的值:

XDocument document = XDocument.Load(@"F:\Appointment Scheduling
  Info\CompleteFacilities.xml");

var locations = from location in document.Descendants("location")
                select new
                {
                    siteCode = location.Element("site_code").Value,
                    phone = location.Element("phone").Value,
                    fax = location.Element("fax").Value,
                    standardized_operation_hours = location.Element("standardized_operation_hours"),
                    timeZone = location.Element("time_zone") != null ? location.Element("time_zone").Value : null,
                    scheduling = location.Element("scheduling") != null ? location.Element("scheduling").Value : null                                
                };

问题: 我可以使用:locations.First()。phone

来检索PhoneNUmber

但是当我访问时,locations.First()。standardsization_operation_hours.Value - 我希望获得子节点值:

<Monday>07:30-17:00</Monday>
<Tuesday>07:30-17:00</Tuesday>
<Wednesday>07:30-17:00</Wednesday>
<Thursday>07:30-17:00</Thursday>
<Friday>07:30-17:00</Friday>
<Saturday>X</Saturday>
<Sunday>X</Sunday>`

检索到的是叶值:“07:30-17:0007:30-17:0007:30-17:0007:30-17:0007:30-17:00XX”

如何使用包含星期一/星期二的完整子节点信息检索上述预期值。?

0 个答案:

没有答案