tags:

views:

83

answers:

2

How to check whether an Xml file have processing Instruction

Example

 <?xml-stylesheet type="text/xsl" href="Sample.xsl"?>

 <Root>
    <Child/>
 </Root>

I need to read the processing Instruction

<?xml-stylesheet type="text/xsl" href="Sample.xsl"?>

from the xml file.

Please help me to do this

+1  A: 

You can use FirstChild property of XmlDocument class and XmlProcessingInstruction class:

        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 or Data properties to get appropriate values.

brain_pusher
+3  A: 

How about:

XmlProcessingInstruction instruction = doc.SelectSingleNode("processing-instruction('xml-stylesheet')") as XmlProcessingInstruction;
Garett
@Garett : Thank you
Pramodh