从具有名称空间的XML文件中读取数据
本文关键字:文件 读取 数据 XML 有名称 空间 | 更新日期: 2023-09-27 18:09:01
在下面的xml中,我需要获得以下值
rfc = " CODJ7002145Q3 UUID = " 07 beef18-45af-5f3b-bb44-c2758802adcf "
<?xml version="1.0" encoding="UTF-8"?>
<cfdi:Comprobante xmlns:cfdi="http://www.sat.gob.mx/cfd/3" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv32.xsd" version="3.2" serie="C" folio="371" fecha="2015-03-04T12:48:04" sello="abnLKgvWQ+pPBW1YSI/N941kf7eDOn8GBttp1/AJAdkQS6n60s37saV9ugTfOpYM4G+Q+4zr0jn0RQldamFyMDo/F3BfPg3R9yG8SD5A45i4Sh3CL/El2AHH/Zkyvft8b+BgFAMCoauvpi61BOCNK1EjEPcro0eZoy8ZRBfv+Xo=" formaDePago="PAGO EN UNA SOLA EXHIBICION" noCertificado="00001000000301540335" certificado="MIIEXTCCA0WgAwIBAgIUMDAwMDEwMDAwMDAzMDE1NDAzMzUwDQYJKoZIhvcNAQEFBQAwggGKMTgwNgYDVQQDDC9BLkMuIGRlbCBTZXJ2aWNpbyBkZSBBZG1pbmlzdHJhY2nDs24gVHJpYnV0YXJpYTEvMC0GA1UECgwmU2VydmljaW8gZGUgQWRtaW5pc3RyYWNpw7NuIFRyaWJ1dGFyaWExODA2BgNVBAsML0FkbWluaXN0cmFjacOzbiBkZSBTZWd1cmlkYWQgZGUgbGEgSW5mb3JtYWNpw7NuMR8wHQYJKoZIhvcNAQkBFhBhY29kc0BzYXQuZ29iLm14MSYwJAYDVQQJDB1Bdi4gSGlkYWxnbyA3NywgQ29sLiBHdWVycmVybzEOMAwGA1UEEQwFMDYzMDAxCzAJBgNVBAYTAk1YMRkwFwYDVQQIDBBEaXN0cml0byBGZWRlcmFsMRQwEgYDVQQHDAtDdWF1aHTDqW1vYzEVMBMGA1U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FICADO" LugarExpedicion="FRANCISCO I. MADERO 2017 , NUEVA, 21100, MEXICALI, MEXICALI, BAJA CALIFORNIA, MEXICO">
<cfdi:Emisor rfc="CODJ7002145Q3" nombre="JORGE CORONA DURAN">
<cfdi:DomicilioFiscal calle="FRANCISCO I. MADERO" noExterior="2017" colonia="NUEVA" localidad="MEXICALI" municipio="MEXICALI" estado="BAJA CALIFORNIA" pais="MEXICO" codigoPostal="21100"/>
<cfdi:RegimenFiscal Regimen="PERSONA FISICA CON ACTIVIDAD EMPRESARIAL Y PROFESIONAL"/>
</cfdi:Emisor>
<cfdi:Receptor rfc="VME990127Q70" nombre="VSF MEXICO S.A. DE C.V., SOFOM, E.N.R.
">
<cfdi:Domicilio calle="AVE SANTA FE
" noExterior="495" colonia="MANZANA B LOTE B1 20
 COL. CRUZ MANCA
" localidad="MEXICO" municipio="CUAJIMALPA" estado="DISTRITO FEDERAL" pais="MEXICO" codigoPostal="05349"/>
</cfdi:Receptor>
<cfdi:Conceptos>
<cfdi:Concepto cantidad="1.00" unidad="No aplica" noIdentificacion="RECU" descripcion="RECUPERACION" valorUnitario="869.39" importe="869.39"/>
</cfdi:Conceptos>
<cfdi:Impuestos totalImpuestosRetenidos="179.67" totalImpuestosTrasladados="139.10">
<cfdi:Retenciones>
<cfdi:Retencion impuesto="ISR" importe="86.94"/>
<cfdi:Retencion impuesto="IVA" importe="92.73"/>
</cfdi:Retenciones>
<cfdi:Traslados>
<cfdi:Traslado impuesto="IVA" tasa="16.00" importe="139.10"/>
</cfdi:Traslados>
</cfdi:Impuestos>
<cfdi:Complemento>
<tfd:TimbreFiscalDigital xmlns:tfd="http://www.sat.gob.mx/TimbreFiscalDigital" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/TimbreFiscalDigital/TimbreFiscalDigital.xsd" selloCFD="abnLKgvWQ+pPBW1YSI/N941kf7eDOn8GBttp1/AJAdkQS6n60s37saV9ugTfOpYM4G+Q+4zr0jn0RQldamFyMDo/F3BfPg3R9yG8SD5A45i4Sh3CL/El2AHH/Zkyvft8b+BgFAMCoauvpi61BOCNK1EjEPcro0eZoy8ZRBfv+Xo=" FechaTimbrado="2015-03-04T14:48:50" UUID="07BEEF18-45AF-5F3B-BB44-C2758802ADCF" noCertificadoSAT="00001000000202864883" version="1.0" selloSAT="pnaIMS1fcN4BIkKmoibQQTYANHJeG+1uvwO32ZC4io0wM/INNnspsig9/R+GrCx+fHwCGmyISlU+WLgmww9G5IpB4YuNywJW1j0ggaFunW2hOyLfgmYC2IJeMPUCxrL8pd64NiyNMYzNb0zI/6miefmzgSnAAFZXAYB3YuAMoPo="/>
</cfdi:Complemento>
</cfdi:Comprobante>
下面是我正在使用的代码,但是我得到了一个异常:表达式必须计算为节点集。
Protected Function ProcessXml(ByVal fileName As String) As Boolean
_import = True
Try
Dim xmlDoc As XmlDocument = New XmlDocument()
Dim invNum As Integer = 0
Dim vdetails As DataObjects.Voucher
xmlDoc.Load(fileName)
Library.Log("Start looping through the invoices " & fileName, LogType.INFOLOG, ImportType.ECOUNSEL_IMP)
Dim nodeLst As XmlNodeList = xmlDoc.SelectNodes("/cfdi:Comprobante/")
For Each vouch As XmlElement In nodeLst
Dim rfc As String = vouch("cfdi:Emisor/rfc").InnerText
Console.WriteLine(rfc)
Next
Catch ex As Exception
Library.Log("eCounsel Imported errored out in processXml: " & ex.ToString(), LogType.ERRORLOG, ImportType.ECOUNSEL_IMP)
_import = False
End Try
ProcessXml = _import
End Function
使用xml linqvb.net
Imports System.Xml
Imports System.Xml.Linq
Module Module1
Const FILENAME As String = "c:'temp'test.xml"
Sub Main()
Dim doc As XDocument = XDocument.Load(FILENAME)
Dim rfc As String = doc.Descendants().Where(Function(x) x.Name.LocalName = "Receptor").Select(Function(x) CType(x.Attribute("rfc"), String)).FirstOrDefault()
Dim UUID As String = doc.Descendants().Where(Function(x) x.Name.LocalName = "TimbreFiscalDigital").Select(Function(x) CType(x.Attribute("UUID"), String)).FirstOrDefault()
End Sub
End Module
c# using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication2
{
class Program
{
const string FILENAME = @"c:'temp'test.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
string rfc = doc.Descendants().Where(x => x.Name.LocalName == "Receptor").Select(x => (string)x.Attribute("rfc")).FirstOrDefault();
string UUID = doc.Descendants().Where(x => x.Name.LocalName == "TimbreFiscalDigital").Select(x => (string)x.Attribute("UUID")).FirstOrDefault();
}
}
}