【问题标题】:Getting value of an attribute within namespace获取命名空间内属性的值
【发布时间】:2013-02-26 18:31:26
【问题描述】:

我正在尝试处理以下 XML:

<rif:Rif xmlns:rif="rif" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" rif:numeroRif="XYZXYZXYZ">
    <rif:Nombre>Nombre</rif:Nombre>
    <rif:AgenteRetencionIVA>SI</rif:AgenteRetencionIVA>
    <rif:ContribuyenteIVA>SI</rif:ContribuyenteIVA>
    <rif:Tasa />
</rif:Rif>

我正在使用下一个代码:

XDocument doc = XDocument.Parse(result);
var q = from item in doc.Descendants()
        let attributeType = item.Attribute("AgenteRetencionIVA").Value
        select item;

我在获取属性rif:AgenteRetencionIVA 时遇到问题。我该怎么做?

【问题讨论】:

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


    【解决方案1】:

    看起来你需要指定自定义命名空间:

    string xml = @"...";
    
    XName nameRif = "rif";
    XDocument doc = XDocument.Parse(xml);
    
    var q = from item in doc.Descendants()
            let attributeType = item.Attribute(nameRif + "AgenteRetencionIVA")
            select item.Value;
    var a = q.ToArray();
    

    【讨论】:

    • @user2112467:从 LINQ 查询中声明 XName 并将其与每个元素名称一起使用。
    • @user2112467:你做错了什么。对我来说很好。查看最近的更新。
    猜你喜欢
    • 2021-09-30
    • 1970-01-01
    • 1970-01-01
    • 2012-01-22
    • 2012-11-10
    • 1970-01-01
    • 2011-06-30
    • 2021-12-14
    相关资源
    最近更新 更多