Getting value of an attribute within namespace

让人想犯罪 __ 提交于 2019-12-08 05:40:37

问题


I am trying to process the following 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>

An I am using the next code:

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

I have problems to get the attribute rif:AgenteRetencionIVA. How do I to do it?


回答1:


Looks like tou need to specify custom namespace:

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();


来源:https://stackoverflow.com/questions/15096621/getting-value-of-an-attribute-within-namespace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!