How to read processing instruction from an XML file using .NET 3.5

前端 未结 3 1596

How to check whether an Xml file have processing Instruction

Example

 

 
          


        
相关标签:
3条回答
  • 2021-01-12 22:43

    How about:

    XmlProcessingInstruction instruction = doc.SelectSingleNode("processing-instruction('xml-stylesheet')") as XmlProcessingInstruction;
    
    0 讨论(0)
  • 2021-01-12 22:46

    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.

    0 讨论(0)
  • 2021-01-12 22:48

    How about letting the compiler do more of the work for you:

    XmlDocument Doc = new XmlDocument();
    Doc.Load(openFileDialog1.FileName);
    
    XmlProcessingInstruction StyleReference = 
        Doc.OfType<XmlProcessingInstruction>().Where(x => x.Name == "xml-stylesheet").FirstOrDefault();
    
    0 讨论(0)
提交回复
热议问题