Comment lire l'instruction de traitement à partir d'un fichier XML en utilisant .NET 3.5
-
29-09-2019 - |
Question
Comment vérifier si un fichier Xml ont le traitement de l'instruction
Exemple
<?xml-stylesheet type="text/xsl" href="Sample.xsl"?>
<Root>
<Child/>
</Root>
Je dois lire l'instruction de traitement
<?xml-stylesheet type="text/xsl" href="Sample.xsl"?>
à partir du fichier XML.
S'il vous plaît aidez-moi à le faire.
La solution
Que diriez-vous:
XmlProcessingInstruction instruction = doc.SelectSingleNode("processing-instruction('xml-stylesheet')") as XmlProcessingInstruction;
Autres conseils
Vous pouvez utiliser la propriété de FirstChild
de classe XmlDocument
et classe 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 propriétés Value
ou Data
pour obtenir des valeurs appropriées.
Que diriez-vous de laisser le compilateur faire plus de travail pour vous:
XmlDocument Doc = new XmlDocument();
Doc.Load(openFileDialog1.FileName);
XmlProcessingInstruction StyleReference =
Doc.OfType<XmlProcessingInstruction>().Where(x => x.Name == "xml-stylesheet").FirstOrDefault();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow