[英]How to read an specific node from an xml
I have a problem reading an XML file ( C# ), the point is that I can read all elements inside cfdi:Comprobante
, but when I want to read for example cfdi:emisor
I'm not able to get information, even using:我在读取 XML 文件( C# )时遇到问题,关键是我可以读取cfdi:Comprobante
所有元素,但是当我想读取例如cfdi:emisor
我无法获取信息,即使使用:
XmlDocument xml = new XmlDocument();
xml.Load(vnt.FileName);
XmlElement root = xml.DocumentElement;
//gets sub total from cfdi:Comprobante
XmlAttribute total = root.GetAttributeNode("subTotal");
// HERE IS THE BIG PROBLEM
XmlAttribute rfc = root.GetAttributeNode("cfdi:Comprobante/cfdi:Emisor/rfc");
string valor = total.InnerXml;
//string rfcE = rfc.InnerText; //HERE IS THE PROBLEM
dataGridView1.Rows[0].Cells[2].Value = valor;
// dataGridView1.Rows[0].Cells[1].Value = valor;
<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante LugarExpedicion="ZAPOPAN, JALISCO, MEXICO" metodoDePago="01" tipoDeComprobante="ingreso" total="190.00" subTotal="163.79" noCertificado="00001000000303469404" formaDePago="PAGO EN UNA SOLA EXHIBICION" sello="TWxkop8XLoDKqh9fZe4VMBnRaf2gwYq7oP33BqLKNbBaAvfAyfAoAJ4rlTuhK3m5k9e/jiWAjTdaJ+RO8FJ6L8d5E79gCPZI2gYcVMr/ulIrIHkfiNs37FLBm87vd/0hhbt252Qefr84ALz0bl0XtsDwh6xTmoX2rzFfowXQWIw=" fecha="2017-01-13T21:23:54" folio="4594" version="3.2" xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd http://www.buzonfiscal.com/ns/addenda/bf/2 http://www.buzonfiscal.com/schema/xsd/Addenda_BF_v20.xsd" xmlns:bfa2="http://www.buzonfiscal.com/ns/addenda/bf/2" xmlns:cfdi="http://www.sat.gob.mx/cfd/3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<cfdi:Emisor nombre="MARIA DEL SOCORRO HERNANDEZ SEDANO" rfc="HES12211HN3">
<cfdi:DomicilioFiscal codigoPostal="45019" pais="MEXICO" estado="JALISCO" municipio="ZAPOPAN" colonia="SAN JUAN DE OCOTAN" noExterior="7484" calle="AV VALLARTA"/>
<cfdi:ExpedidoEn codigoPostal="43319" pais="MEXICO" estado="JALISCO" municipio="ZAPOPAN" colonia="SAN JUAN DE OCOTAN" noExterior="7484" calle="AV VALLARTA"/>
<cfdi:RegimenFiscal Regimen="PERSONA FISICA CON ACTIVIDAD EMPRESARIAL"/>
</cfdi:Emisor>
<cfdi:Receptor nombre="SERVICIOS ADMINISTRATIVOS GRACIDA SC" rfc="SAG101208EJ4">
<cfdi:Domicilio codigoPostal="45019" pais="MEXICO" estado="JALISCO" municipio="ZAPOPAN" colonia="ZAPOPAN" noExterior="401" calle="AV PROLONGACION VALLARTA"/>
</cfdi:Receptor>
<cfdi:Conceptos>
<cfdi:Concepto importe="163.79" valorUnitario="163.79" descripcion="CONSUMO" noIdentificacion="001" unidad="No Aplica" cantidad="1"/>
</cfdi:Conceptos>
<cfdi:Impuestos totalImpuestosTrasladados="26.21">
<cfdi:Traslados>
<cfdi:Traslado importe="26.21" tasa="16.00" impuesto="IVA"/>
</cfdi:Traslados>
</cfdi:Impuestos>
You need to add a namespace table to your document:您需要在文档中添加一个命名空间表:
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("cfdi", "http://www.sat.gob.mx/cfd/3");
Also, your XPath query looks a little suspect to me.此外,您的 XPath 查询对我来说有点可疑。 I would try this:我会试试这个:
XmlElement emisor = (XmlElement)root.SelectSingleNode("cfdi:Emisor", nsmgr);
XmlAttribute rfc = emisor.GetAttributeNode("rfc");
Modified code sample:修改后的代码示例:
XmlDocument xml = new XmlDocument();
xml.Load(vnt.FileName);
// namespace manager added here
XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
nsmgr.AddNamespace("cfdi", "http://www.sat.gob.mx/cfd/3");
XmlElement root = xml.DocumentElement;
//gets sub total from cfdi:Comprobante
XmlAttribute total = root.GetAttributeNode("subTotal");
// HERE IS THE BIG PROBLEM
// modified XPath query
XmlElement emisor = (XmlElement)root.SelectSingleNode("cfdi:Emisor", nsmgr);
XmlAttribute rfc = emisor.GetAttributeNode("rfc");
string valor = total.InnerXml;
//string rfcE = rfc.InnerText; //HERE IS THE PROBLEM
dataGridView1.Rows[0].Cells[2].Value = valor;
// dataGridView1.Rows[0].Cells[1].Value = valor;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.