[英]What is the best way to read an attribute from an xml string in c#
我有以下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>
我想讀取節點tfd中的屬性UUID:TimbreFiscalDigital所以我想知道如何使用c#來做這個,這可能很傻但請理解我是c#的新手。
注意:這個xml在一個字符串里面,而不是在一個文件中(我們的提供者的webservice將xml作為字符串返回,不是我們的錯)
注2:我可以使用Linq或任何其他庫,這不是一個概率
謝謝!!
我將它包裝在聲明命名空間的根節點中。 我也使用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();
}
}
我發現linq的XDocument
和相關類特別簡單易用:
string uuid = XDocument.Parse(xmlString)
.Descendants("TimbreFiscalDigital")
.Attributes("UUID")
.First()
.Value;
因為您有名稱空間前綴,所以您必須使用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");
進一步閱讀:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.