Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

xml

asp.net

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!!

like image 638
franko_camron Avatar asked Oct 06 '11 00:10

franko_camron


3 Answers

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();
    }
}
like image 192
TheCloudlessSky Avatar answered Oct 21 '22 07:10

TheCloudlessSky


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
like image 34
user7116 Avatar answered Oct 21 '22 07:10

user7116


I find linq's XDocument and related classes to be particularly simple and straightforward to use:

string uuid = XDocument.Parse(xmlString)
    .Descendants("TimbreFiscalDigital")
    .Attributes("UUID")
    .First()
    .Value;
like image 27
gilly3 Avatar answered Oct 21 '22 07:10

gilly3