XSLT的价值不会加载数据

时间:2019-09-10 12:10:36

标签: xml asp.net-mvc entity-framework xslt serialization

我正在构建asp.net mvc 5 Web应用程序,并且进入了需要进行一些序列化的过程。

我有一个带有post方法的表单,我需要将表单中的数据保存到PDF文件中,为此,我将这些数据保存在XML文件中,然后将该文件转换为HTML使用XSLT,最后将其转换为PDF。

现在一切正常,但是当我打开PDF文件查看是否存储了数据时,什么也看不到,只有我用XSLT文件制作的结构。

我相信我对XSLT文件做错了,这里是:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">


<xsl:template match="/">
  <html>
    <body>
      <h2>Adherent</h2>
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>numero adhesion</th>
            <th>nom</th>
            <th>numero adhesion</th>
            <th>nom</th>
          </tr>
          <tr>
            <td>
              <xsl:value-of select="NumeroAdhesion"/>
            </td>
            <td>
              <xsl:value-of select="Nom"/>
            </td>
            <td>TEST</td>
            <td>TEST</td>
          </tr>
        </table>
    </body>
  </html>
</xsl:template>

这是同时生成XML和PDF的代码:

    public static void SaveDataToFile(Adherent adherent)
    {

        System.Xml.Serialization.XmlSerializer writer = new System.Xml.Serialization.XmlSerializer(typeof(Adherent));
        var path = @"C:\Users\LUCAS\Desktop\Affiliation\BulletinAdhesion\eSign\XML\" + adherent.NumeroAdhesion + ".xml";
        System.IO.FileStream file = System.IO.File.Create(path);

        writer.Serialize(file, adherent);

        file.Close();
        CreatePDF(adherent.NumeroAdhesion);
    }
    public static void CreatePDF(string filename)
    {


        XslCompiledTransform transform = new XslCompiledTransform();
        using (XmlReader reader = XmlReader.Create(@"C:\Users\LUCAS\Desktop\Affiliation\BulletinAdhesion\eSign\XSLT\XSLTFile1.xslt"))
        {
            transform.Load(reader);
        }
        StringWriter results = new StringWriter();
        using (XmlReader reader = XmlReader.Create(@"C:\Users\LUCAS\Desktop\Affiliation\BulletinAdhesion\eSign\XML\"+ filename +".xml"))
        {
            transform.Transform(reader, null, results);
        }
        IronPdf.HtmlToPdf Renderer = new IronPdf.HtmlToPdf();
        Renderer.RenderHtmlAsPdf(results.ToString()).SaveAs(@"C:\Users\LUCAS\Desktop\Affiliation\BulletinAdhesion\eSign\PDF\" + filename + ".pdf");

    }

更新: 这是XML文件

enter image description here

更新: 这是XML文件作为代码示例:

<?xml version="1.0"?>
<Adherent xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<AdherentId>0</AdherentId>
<NumeroAdhesion>AD000001</NumeroAdhesion>
<NumeroPolice>AD000001</NumeroPolice>
<Nom>LOUKACH</Nom>
<Prenom>EL-MEHDI</Prenom>
<DateNaissance>1998-01-05T00:00:00</DateNaissance>
<Sexe>Homme</Sexe>
<SituationFamille>Celibataire</SituationFamille>
<Cin>L700878</Cin>
<Adresse>Av allal fassi res yasmin wilaya</Adresse>
<Telephone>+212662186047</Telephone>
<Departement>IT</Departement>
<Direction>Casablanca</Direction>
<Secteur>Informatique</Secteur>
<Fonction>Développeur Web</Fonction>
<DateFonction>2019-07-15T00:00:00</DateFonction>
<DateAdhesion>2019-09-10T00:00:00</DateAdhesion>
<NumeroMatricule>1904</NumeroMatricule>
<Salaire>3000</Salaire>
<ConjointId xsi:nil="true" />
<Conjoints>
<ConjointId>0</ConjointId>
<Sexe xsi:nil="true" />
<DateNaissance xsi:nil="true" />
</Conjoints>
<EnfantId xsi:nil="true" />
<Enfants>
<EnfantId>0</EnfantId>
<Sexe xsi:nil="true" />
<DateNaissance xsi:nil="true" />
</Enfants>
<BeneficiaireId xsi:nil="true" />
<BeneficiaireEnCasDeces>
<BeneficiaireId>0</BeneficiaireId>
<Sexe xsi:nil="true" />
<DateNaissance xsi:nil="true" />
<BeneficiaireAyantDroit>false</BeneficiaireAyantDroit>
</BeneficiaireEnCasDeces>
</Adherent>

1 个答案:

答案 0 :(得分:1)

您的路径表达式NumeroAdhesionNom没有选择任何内容。仅当XML的顶级元素与此名称匹配时,他们才会选择任何内容(并且只有一个顶级元素,因此它不能同时匹配两个名称)。我怀疑这些名称出现在文档树的深处,您需要一个更特定的路径来选择它们。不用看原始文档,我们真正可以告诉您的一切。

更新

现在您已经发布了XML源,我们可以看到正确的路径是Adherent/NumeroAdhesionAdherent/Nom