在Linq中添加样式表对XML文档的引用?

Ste*_*veO 8 .net xml linq-to-xml

我创建了一个XML Doc,并希望有一个对XSLT文件的引用.

//<?xml-stylesheet type="text/xsl" href="OBReport.xslt"?>
Run Code Online (Sandbox Code Playgroud)

这个XML生成:

XElement xml = new XElement("ReportedOn",
                    from dl in EL.DocumentLog.ToList()
                    join o in EL.Organization
                    on dl.OrganizationID equals o.OrganizationId
                    where dl.ActionDate >= stDate &
                    dl.ActionDate <= enDate 
                    orderby dl.DefendantName, dl.DocumentName
                    select new XElement("persons",
                              new XAttribute("documentName", dl.DocumentName),
                              new XElement("defendantName", dl.DefendantName),
                              new XElement("actionDate", dl.ActionDate.ToString()),
                              new XElement("startDate", dl.StartDate.ToString()),
                           new XElement("endDate", dl.EndDate.ToString()),
                           new XElement("organizationName" , o.OrganizationName) ));
Run Code Online (Sandbox Code Playgroud)

Hen*_*man 12

添加XProcessingInstruction元素.

而不是你的XElement(可以用作文档,但有限制),而不是一个包络的XDocument.那么,在您的代码之后:

 XElement body = ...; // root XElement from your Linq statement 
 XDocument doc = new XDocument(
      new XProcessingInstruction("xml-stylesheet", "type='text/xsl' ref='hello.xsl'"), 
      body);  
Run Code Online (Sandbox Code Playgroud)