c# - Linq to XML 条件属性

标签 c# xml linq

这是我的 xml

<DataSet xmlns="http://www.bnr.ro/xsd" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://www.bnr.ro/xsd nbrfxrates.xsd">
    <Header>
        <Publisher>National Bank of Romania</Publisher>
        <PublishingDate>2016-03-24</PublishingDate>
    <MessageType>DR</MessageType>
    </Header>
    <Body>
        <Subject>Reference rates</Subject>
        <OrigCurrency>RON</OrigCurrency>
        <Cube date="2016-03-24">            
            <Rate currency="EUR">4.4655</Rate>          
        </Cube>
        <Cube date="2016-03-23">                
            Rate currency="EUR">4.4641</Rate>               
        </Cube>
    </Body>
</DataSet>

我想验证多维数据集属性日期以接收昨天日期的欧元值。

例如,如果今天是 2016-03-24,我希望收到 2016-03-23 的值 4.4641

我尝试使用 LINQ to XML

    string date_yesterday = DateTime.Now.AddDays(-1).Date.ToString("yyyy-MM-dd");
XElement root = XElement.Parse(sbXmlText.ToString());
                IEnumerable<XElement> adress =
                    from el in root.Descendants("Cube")
                    let z = el.ElementsAfterSelf().FirstOrDefault()
                    where z != null && (string)el.Attribute("date") == date_yesterday
                    select el;
                foreach (XElement el in adress)
                    Console.WriteLine(el);

并尝试过

string date_yesterday = DateTime.Now.AddDays(-1).Date.ToString("yyyy-MM-dd");

             XElement root = XElement.Parse(sbXmlText.ToString());
             IEnumerable<XElement> adress =
                 root.Descendants("Cube").Where(r => r.Attribute("date").Value == date_yesterday);                    
             foreach (XElement el in adress)
                 Console.WriteLine(el);

每次都会返回 null

最佳答案

您的 XML 有默认命名空间。您可以使用XNamespace+元素的本地名称”来引用命名空间中的元素,例如:

var xml = @"<DataSet xmlns='http://www.bnr.ro/xsd' 
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
 xsi:schemaLocation='http://www.bnr.ro/xsd nbrfxrates.xsd'>
    <Header>
        <Publisher>National Bank of Romania</Publisher>
        <PublishingDate>2016-03-24</PublishingDate>
    <MessageType>DR</MessageType>
    </Header>
    <Body>
        <Subject>Reference rates</Subject>
        <OrigCurrency>RON</OrigCurrency>
        <Cube date='2016-03-24'>            
            <Rate currency='IDR'>1.1111</Rate>
            <Rate currency='EUR'>4.4655</Rate>          
        </Cube>
        <Cube date='2016-03-23'>                
            <Rate currency='EUR'>4.4641</Rate>               
        </Cube>
    </Body>
</DataSet>";
var doc = XDocument.Parse(xml);

//XNamespace that reference default namespace URI:
XNamespace d = "http://www.bnr.ro/xsd";

var yesterday = DateTime.Now.AddDays(-1).Date;

//Use `XNamespace+element's local-name` to reference element in namespace:
var result = (from cube in doc.Descendants(d+"Cube")
              from rate in cube.Elements(d+"Rate")
              where 
                ((DateTime)cube.Attribute("date")).Date == yesterday
                    && 
                (string)rate.Attribute("currency") == "EUR"
              select (decimal)rate
              ).FirstOrDefault();
Console.WriteLine(result);

输出:

4.4641

关于c# - Linq to XML 条件属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36199491/

相关文章:

c# - 如何存储对基于传递给方法的表达式创建的泛型类型的引用?

c# - 在 C# 中打印字典键值对

c# - 当页面在单击按钮时加载动态内容时,页脚 CSS 不起作用

javascript - xmlhttp.open 多个 XML 文件

c# - 使用 Entity Framework、LINQ 进行预加载

c# - 在 ForEach 函数中模拟 break 语句

c# - XPath 和属性

java - 将存储在 SQLite 数据库中的经度和纬度数据导出到文件以便可以通过网站将其导入 Google Map API 的最简单方法?

c# - 切换 linq 语法

c# - 没有 PredicateBuilder 的 LINQ 中的动态或