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

时间:2010-09-09 21:01:02

标签: .net xml linq-to-xml

我创建了一个XML Doc,并且想要引用XSLT文件。

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

到这个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) ));

1 个答案:

答案 0 :(得分: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);