Extract attribute data from an XML file.

Namespace:  FirmaSAT
Assembly:  diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.50.0.29531

Syntax

C#
public static string GetXmlAttribute(
	string xmlFile,
	string attributeName,
	string elementName
)
Visual Basic (Declaration)
Public Shared Function GetXmlAttribute ( _
	xmlFile As String, _
	attributeName As String, _
	elementName As String _
) As String

Parameters

xmlFile
Type: System..::.String
Name of input XML file to be processed (or a string containing XML data)
attributeName
Type: System..::.String
Name of attribute
elementName
Type: System..::.String
Name of element or xpath expression (see remarks)

Return Value

Attribute value, if found; or "!NO MATCH!" if not found (see XmlNoMatch()()()); or empty string on error (the empty string may be the correct result if attr="", so check for an error using LastError()()()).

Remarks

For a simple element name, the default behaviour is to get the attribute from the first element found with the given name. Specify the element name in the form "element[N]" to extract the attribute for the N'th element found in the XML document, where N is a positive decimal integer (N=1,2,3,...). Setting elementName="" will output the value of the named attribute from the root element of the XML document. Setting both elementName="" and attributeName="" will output the name of the root element itself.

XPath expression: Alternatively, specify an path expression in elementName using the "/" and "//" operators and optional predicate [N] where N is a positive integer. For example "/Comprobante/Emisor" or "//Concepto[2]//Retencion[3]". This is a simplified form of XPath that selects the first occurrence of the element matching the path expression (whereas XPath would select all matching elements). Path expressions must start with a "/" or "//" and must not end with a "/". No other XPath function or operator is accepted. Do not use namespace prefixes (e.g. "cfdi:") in the path expression.

Simplified Xpath syntax:

  • /e1 -- selects the first <e1> document element (child element of the document node).
  • /e1/e2 -- selects the first <e2> child element of the first <e1> document element.
  • /e1[2]/e2[3] -- selects the third <e2> child element of the second <e1> document element.
  • /e1[1]/e2[1] -- same as /e1/e2.
  • //e2 -- the first <e2> element found anywhere (same as simple e2).
  • //e2[3] -- the third <e2> element found anywhere (same as simple e2[3]).
  • /e1//e2 -- the first <e2> element found anywhere inside the <e1> element.

To test for the existence of an element, set attributeName="". This will return the name of the element if it exists, or "!NO MATCH!" if not found. This message can be changed using SetXmlNoMatch(String).

Examples

CopyC#
Console.WriteLine(Sat.GetXmlAttribute("cfdv40-ejemplo.xml", "Nombre", "cfdi:Emisor"));
// Esta es una demostración

See Also