代码之家  ›  专栏  ›  技术社区  ›  Thorin Oakenshield

如何使用.NET3.5从XML文件中读取处理指令

  •  5
  • Thorin Oakenshield  · 技术社区  · 14 年前

    例子

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

    我需要阅读处理说明

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

    从XML文件。

    请帮我做这个。

    3 回复  |  直到 13 年前
        1
  •  17
  •   Garett    14 年前

    怎么样:

    XmlProcessingInstruction instruction = doc.SelectSingleNode("processing-instruction('xml-stylesheet')") as XmlProcessingInstruction;
    
        2
  •  5
  •   bluish dmajkic    13 年前

    你可以用 FirstChild 财产 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);
    }
    

    解析 Value Data 属性以获取适当的值。

        3
  •  0
  •   Better late than never    8 年前

    XmlDocument Doc = new XmlDocument();
    Doc.Load(openFileDialog1.FileName);
    
    XmlProcessingInstruction StyleReference = 
        Doc.OfType<XmlProcessingInstruction>().Where(x => x.Name == "xml-stylesheet").FirstOrDefault();