简体   繁体   English

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

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

I have the following xml as string: 我有以下xml作为字符串:

<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#. 我想读取节点tfd中的属性UUID:TimbreFiscalDigital所以我想知道如何使用c#来做这个,这可能很傻但请理解我是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) 注意:这个xml在一个字符串里面,而不是在一个文件中(我们的提供者的webservice将xml作为字符串返回,不是我们的错)

Note2: I can use Linq, or any other library, that's not a prob 注2:我可以使用Linq或任何其他库,这不是一个概率

Thanks!! 谢谢!!

I wrapped this in a root node declaring the namespaces. 我将它包装在声明命名空间的根节点中。 I'm also using XPath to query for the node. 我也使用XPath来查询节点。

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

I find linq's XDocument and related classes to be particularly simple and straightforward to use: 我发现linq的XDocument和相关类特别简单易用:

string uuid = XDocument.Parse(xmlString)
    .Descendants("TimbreFiscalDigital")
    .Attributes("UUID")
    .First()
    .Value;

Because you have namespace prefixes, you'll have to use XNamespace instances to help you reference the elements. 因为您有名称空间前缀,所以您必须使用XNamespace实例来帮助您引用元素。

// 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: 进一步阅读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM