What is the best way to read an attribute from an xml string in c#

I have the following xml as string:

<cfdi:Comprobante version="3.0"
                  xsi:schemaLocation="http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv3.xsd"
                  serie="A"
                  folio="6"
                  fecha="2011-07-22T13:51:42"
                  formaDePago="Pago en una sola exhibición"
                  sello="XlSJYAxauwYbI"
                  noCertificado="00001000000101242210"
                  certificado="YtEQOHw02OGx6E="
                  condicionesDePago="Paguese a mas tardar el 21/08/2011."
                  subTotal="123"
                  Moneda="MXN"
                  total="123"
                  tipoDeComprobante="ingreso">
  <cfdi:Complemento>
    <tfd:TimbreFiscalDigital FechaTimbrado="2011-07-22T13:51:47"
                             UUID="41C8A54F-4956-1BAD-F2CB-48E8343918FD"
                             noCertificadoSAT="00001000000102616613"
                             selloCFD="wrwerewe"
                             version="1.0"
                             xsi:schemaLocation="http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/timbrefiscaldigital/TimbreFiscalDigital.xsd"/>
  </cfdi:Complemento>
</cfdi:Comprobante>

I want to read the attribute UUID inside the node tfd:TimbreFiscalDigital so I was wondering how to do this using c#, this might be silly but please understand I'm new in c#.

Note: This xml is inside a string, not in a file (our provider's webservice returns the xml as string, is not our fault)

Note2: I can use Linq, or any other library, that's not a prob

Thanks!!


Because you have namespace prefixes, you'll have to use XNamespace instances to help you reference the elements.

// We use these to establish our namespace prefixes
XNamespace cfdi = @"http://www.sat.gob.mx/cfd/3";
XNamespace tfd = @"http://www.sat.gob.mx/TimbreFiscalDigital";

var xdoc = XDocument.Parse(xml);

// Walk down the XML tree to tfd:TimbreFiscalDigital
var elt = xdoc.Element(cfdi + "Comprobante")
              .Element(cfdi + "Complemento")
              .Element(tfd + "TimbreFiscalDigital");
// Alternately
// var elt = xdoc.Descendants(tfd + "TimbreFiscalDigital")
//               .First();

var uuid = (string)elt.Attribute("UUID");

// You can convert attributes and element values to lots of built-in types
// See the Explicit Conversions for XAttribute and XElement on MSDN
var date = (DateTime)elt.Attribute("FechaTimbrado");

Further reading:

  • MSDN: XElement Operators
  • MSDN: XAttribute Operators

  • I wrapped this in a root node declaring the namespaces. I'm also using XPath to query for the node.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml.Linq;
    using System.Xml.XPath;
    using System.Xml;
    
    class Program
    {
        static void Main(string[] args)
        {
    
            var doc = @"
    <Root xmlns:xsi='http://someuri' xmlns:cfdi='http://someuri2' xmlns:tfd='http://someuri3'>
    <cfdi:Comprobante version='3.0'
                      xsi:schemaLocation='http://www.sat.gob.mx/cfd/3 http://www.sat.gob.mx/sitio_internet/cfd/3/cfdv3.xsd'
                      serie='A'
                      folio='6'
                      fecha='2011-07-22T13:51:42'
                      formaDePago='Pago en una sola exhibición'
                      sello='XlSJYAxauwYbI'
                      noCertificado='00001000000101242210'
                      certificado='YtEQOHw02OGx6E='
                      condicionesDePago='Paguese a mas tardar el 21/08/2011.'
                      subTotal='123'
                      Moneda='MXN'
                      total='123'
                      tipoDeComprobante='ingreso'>
      <cfdi:Complemento>
        <tfd:TimbreFiscalDigital FechaTimbrado='2011-07-22T13:51:47'
                                 UUID='41C8A54F-4956-1BAD-F2CB-48E8343918FD'
                                 noCertificadoSAT='00001000000102616613'
                                 selloCFD='wrwerewe'
                                 version='1.0'
                                 xsi:schemaLocation='http://www.sat.gob.mx/TimbreFiscalDigital http://www.sat.gob.mx/sitio_internet/timbrefiscaldigital/TimbreFiscalDigital.xsd'/>
      </cfdi:Complemento>
    </cfdi:Comprobante>
    </Root>";
    
            var uuid = XDocument.Parse(doc)
                var uuid = XDocument.Parse(doc)
                .XPathSelectElement("//*[local-name() = 'TimbreFiscalDigital']")
                .Attribute("UUID").Value;
    
            // Work with uuid
    
            Console.Read();
        }
    }
    

    我发现linq的XDocument和相关类特别简单易用:

    string uuid = XDocument.Parse(xmlString)
        .Descendants("TimbreFiscalDigital")
        .Attributes("UUID")
        .First()
        .Value;
    
    链接地址: http://www.djcxy.com/p/55226.html

    上一篇: 试图将[NSImage imageNamed:NSImageNameUser]转换为NSData

    下一篇: 从c#中的xml字符串读取属性的最佳方法是什么