Dot-Net

在 Linq 中添加對 XML 文件的樣式表引用?

  • January 17, 2021

我創建了一個 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) ));

添加一個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);  

引用自:https://stackoverflow.com/questions/3680559