Cómo leer la instrucción de procesamiento de un archivo XML utilizando .NET 3.5
-
29-09-2019 - |
Pregunta
¿Cómo comprobar si un archivo XML han instrucción de procesamiento
Ejemplo
<?xml-stylesheet type="text/xsl" href="Sample.xsl"?>
<Root>
<Child/>
</Root>
Necesito leer la instrucción de procesamiento
<?xml-stylesheet type="text/xsl" href="Sample.xsl"?>
en el archivo XML.
Por favor me ayude a hacerlo.
Solución
¿Qué hay de:
XmlProcessingInstruction instruction = doc.SelectSingleNode("processing-instruction('xml-stylesheet')") as XmlProcessingInstruction;
Otros consejos
Puede utilizar la propiedad FirstChild
de la clase y la clase XmlDocument
XmlProcessingInstruction
:
XmlDocument doc = new XmlDocument();
doc.Load("example.xml");
if (doc.FirstChild is XmlProcessingInstruction)
{
XmlProcessingInstruction processInfo = (XmlProcessingInstruction) doc.FirstChild;
Console.WriteLine(processInfo.Data);
Console.WriteLine(processInfo.Name);
Console.WriteLine(processInfo.Target);
Console.WriteLine(processInfo.Value);
}
Parse Value
o Data
propiedades para obtener valores apropiados.
¿Qué tal dejar que el compilador de hacer más del trabajo por usted:
XmlDocument Doc = new XmlDocument();
Doc.Load(openFileDialog1.FileName);
XmlProcessingInstruction StyleReference =
Doc.OfType<XmlProcessingInstruction>().Where(x => x.Name == "xml-stylesheet").FirstOrDefault();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow