SatGetXmlAttribute Method |
Extract attribute data from an XML file.
Namespace: FirmaSATAssembly: diFirmaSatNet (in diFirmaSatNet.dll) Version: 10.70.0.38178 (10.70.0.0)
Syntaxpublic static string GetXmlAttribute(
string xmlFile,
string attributeName,
string elementName
)
Public Shared Function GetXmlAttribute (
xmlFile As String,
attributeName As String,
elementName As String
) As String
Parameters
- xmlFile String
- Name of input XML file to be processed (or a string containing XML data)
- attributeName String
- Name of attribute
- elementName String
- Name of element or xpath expression (see remarks)
Return Value
StringAttribute 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).
RemarksFor 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).
ExampleConsole.WriteLine(Sat.GetXmlAttribute("cfdv40-ejemplo.xml", "Nombre", "cfdi:Emisor"));
See Also