问题
I have the following XML file
<?xml version="1.0" encoding="utf-8"?>
<Comprobante version="2.2" serie="A" folio="35207" fecha="2013-05-31T11:51:48">
<Emisor rfc="" nombre="E">
<DomicilioFiscal calle="" noExterior="" colonia="" />
<ExpedidoEn calle="" noExterior="" colonia="" />
<RegimenFiscal Regimen="Regimen" />
</Emisor>
<Receptor rfc="" nombre="Z">
<Domicilio calle="" noExterior="" colonia="" />
</Receptor>
<Conceptos cantidad="1.000" unidad="COMISION" descripcion="PENDIENTE" valorUnitario="28.50000" importe="28.50" />
<Impuestos totalImpuestosTrasladados="3.14">
<Traslados>
<Traslado impuesto="IVA" tasa="11.00" importe="3.14" />
</Traslados>
</Impuestos>
<Addenda>
<ener:EstadoDeCuentaCombustible xmlns:ener="">
<ener:cadenaOriginal>||2.2|A|35207|2013-05-31T11:51:48|773463|2011|ingreso|Pago en una sola exhibicion|28.50|31.64|Tarjeta|Tijuana,Baja California|3213|ERE|E S.A. de C.V.|Prol|13351|Anexa e|Tijuana|Tijuana|Baja California|Mexico|22100|Prol|13351|Anexa e|Tijuana|Tijuana|Baja California|Mexico|22100|Regimen|XA|Z||||TIJUANA|TIJUANA|BAJA CALIFORNIA|Mexico||1.000|COMISION|PENDIENTE|28.50000|28.50|IVA|11.00|3.14|3.14||</ener:cadenaOriginal>
<ener:idRefund>98</ener:idRefund>
</ener:EstadoDeCuentaCombustible>
</Addenda>
</Comprobante>
I need to get the text that is inside (that long string)
Here's how I start the c# code
XmlDocument doc = new XmlDocument();
doc.Load("Route");
XmlNamespaceManager xnm = new XmlNamespaceManager(doc.NameTable);
xnm.AddNamespace("Documento", "http://www.sat.gob.mx/cfd/2");
xnm.AddNamespace("ener", "http://www.enercard.com.mx/cfd");
I've tried various ways...
//strOriginalString = doc.DocumentElement.SelectSingleNode("//Documento:Addenda", xnm).FirstChild.SelectSingleNode("//ener:cadenaOriginal", xnm).InnerText;
//strOriginalString = doc.DocumentElement.SelectSingleNode("//Documento:Addenda//ener:EstadoDeCuentaCombustible", xnm).FirstChild.SelectSingleNode("//ener:cadenaOriginal", xnm).InnerText;
//strOriginalString = doc.DocumentElement.SelectSingleNode("//Documento:Addenda/Documento:cadenaOriginal", xnm).InnerXml;
this 3 ways always throw an exception...
I found another way that doesn't throw an exception, but it doesn't get the string
XmlElement root = doc.DocumentElement;
XmlNodeList nodes = root.SelectNodes("/Addenda/EstadoDeCuentaCombustible");
strOriginalString = "";
foreach (XmlNode node in nodes)
{
XmlNode child = node.SelectSingleNode("./cadenaOriginal");
if (child != null)
{
strOriginalString = child.InnerText;
break;
}
}
What am I doing wrong? or is there another way that I can get the string inside
回答1:
This is somewhat simplified, and you'll need to deal with the namespace problems (one I note below), but otherwise, this is the basic construct:
XmlDocument doc = new XmlDocument();
try { doc.Load("c:\\temp\\test.xml"); }
catch (Exception ex) { }
XmlElement root = doc.DocumentElement;
String strOriginalString = "";
foreach (XmlNode node in root.SelectNodes("/Comprobante/Addenda"))
{
XmlNode child = node.SelectSingleNode("EstadoDeCuentaCombustible/cadenaOriginal");
if (child != null)
{
strOriginalString = child.InnerText;
break;
}
}
There's an issue with <ener:EstadoDeCuentaCombustible xmlns:ener="">
as the empty namespace is invalid.
回答2:
You're missing xmlns:ener
declaration on your XML document:
<ener:EstadoDeCuentaCombustible xmlns:ener="http://www.enercard.com.mx/cfd">
Fix that and you'll be able to use something like this:
string xpath = "/Comprobante/Addenda/ener:EstadoDeCuentaCombustible";
foreach (XmlNode estado in doc.SelectNodes(xpath, xnm))
{
Console.WriteLine("ener:cadenaOriginal={0}",
estado.SelectSingleNode("ener:cadenaOriginal", xnm).InnerText);
}
回答3:
I find Linq2Xml easier to use. (Assuming you have a valid namespace in xmlns:ener=""
).
var xDoc = XDocument.Load(filename);
XNamespace ener = "your name space for ex ,http://www.enercard.com.mx/cfd";
var result = xDoc.Descendants(ener + "cadenaOriginal").First().Value;
回答4:
Your second attempt looks the closest to what should work:
//strOriginalString = doc.DocumentElement.SelectSingleNode("//Documento:Addenda//ener:EstadoDeCuentaCombustible", xnm).FirstChild.SelectSingleNode("//ener:cadenaOriginal", xnm).InnerText;
But switch to this(edited):
//strOriginalString = doc.SelectSingleNode("//ener:cadenaOriginal", xnm).InnerText;
来源:https://stackoverflow.com/questions/16946475/how-to-get-innertext-in-an-xml