Add Stylesheet reference to XML Document in Linq?

核能气质少年 提交于 2020-01-03 08:28:10

问题


I create an XML Doc and wanted have a reference to the XSLT file.

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

to this XML generation:

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) ));

回答1:


Add an XProcessingInstruction element.

And not to your XElement (which can be used as a document but with limitations) but to an enveloping XDocument. So, after your code:

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


来源:https://stackoverflow.com/questions/3680559/add-stylesheet-reference-to-xml-document-in-linq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!