【问题标题】:How to get list of specific XML elements from XML document using C# ASP.Net?如何使用 C# ASP.Net 从 XML 文档中获取特定 XML 元素的列表?
【发布时间】:2020-07-10 08:32:39
【问题描述】:

我正在尝试从 XML 文档中获取一组特定的元素,以使用 XSLT 文件显示。

我的代码是成功的,但是,我不知道如何根据选择的变化显示特定元素,而不是显示整个 XML 文档。

我的 C# 代码:

string strXSLTFile = Server.MapPath("EmployeeXSLT.xslt");
string strXMLFile = Server.MapPath("Employess.xml");

XmlReader reader = XmlReader.Create(strXMLFile);

XslCompiledTransform objXSLTransform = new XslCompiledTransform();
objXSLTransform.Load(strXSLTFile);

StringBuilder htmlOutput = new StringBuilder();
TextWriter htmlWriter = new StringWriter(htmlOutput);
objXSLTransform.Transform(reader, null, htmlWriter);
ltRss.Text = htmlOutput.ToString();
reader.Close();

我的 XML:

<?xml version="1.0" encoding="utf-8" ?>
<Employees>
  <Employee type="1">
    <ID>100</ID>
    <FirstName>Bala</FirstName>
    <LastName>Murugan</LastName>
    <Dept>Production Support</Dept>
  </Employee>
  <Employee type="2">
    <ID>101</ID>
    <FirstName>Peter</FirstName>
    <LastName>Laurence</LastName>
    <Dept>Development</Dept>
  </Employee>
  <Employee type="3">
    <ID>102</ID>
    <FirstName>Rick</FirstName>
    <LastName>Anderson</LastName>
    <Dept>Sales</Dept>
  </Employee>
  <Employee type="4">
    <ID>103</ID>
    <FirstName>Ramesh</FirstName>
    <LastName>Kumar</LastName>
    <Dept>HR</Dept>
  </Employee>
  <Employee type="5">
    <ID>104</ID>
    <FirstName>Katie</FirstName>
    <LastName>Yu</LastName>
    <Dept>Recruitment</Dept>
  </Employee>
  <Employee type="6">
    <ID>105</ID>
    <FirstName>Suresh</FirstName>
    <LastName>Babu</LastName>
    <Dept>Inventory</Dept>
  </Employee>
</Employees>

我的 XSLT:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <xsl:for-each select="//Employee">
      <div style="border:1px black solid;width:300px;margin:1px">
        <div>
          <b>Employee ID:</b>
          <xsl:value-of select="ID"/>
        </div>
        <div>
          <b>Name:</b>
          <xsl:value-of select="FirstName"/>
          <xsl:text> </xsl:text>
          <xsl:value-of select="LastName"/>
        </div>
        <div>
          <b>Department:</b>
          <xsl:value-of select="Dept"/>
        </div>
      </div>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>

HTML:

<asp:Literal ID="ltRss" runat="server"></asp:Literal>

同样,我需要使用 C# 代码来根据用户选择来选择特定数据。假设特定员工的“type = 3”。

【问题讨论】:

    标签: c# asp.net xml xslt


    【解决方案1】:

    使用字典:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            const string FILENAME = @"c:\temp\test.xml";
            static void Main(string[] args)
            {
                XmlReader reader = XmlReader.Create(FILENAME);
                XmlSerializer serializer = new XmlSerializer(typeof(Employees));
                Employees employees = (Employees)serializer.Deserialize(reader);
    
                Dictionary<int, Employee> dict = employees.employees
                    .GroupBy(x => x.ID, y => y)
                    .ToDictionary(x => x.Key, y => y.FirstOrDefault());
    
                Employee e100 = dict[100];
            }
        }
        public class Employees
        {
            [XmlElement("Employee")]
            public Employee[] employees { get; set; }
        }
    
        public class Employee
        {
            public int ID { get; set;}
            public string FirstName { get; set;}
            public string LastName { get; set;}
            public string Dept { get; set;}
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 1970-01-01
      • 2019-12-28
      • 2018-11-30
      • 2013-11-06
      相关资源
      最近更新 更多