【问题标题】:bind dropdown with XML file将下拉列表与 XML 文件绑定
【发布时间】:2010-11-30 17:49:27
【问题描述】:

我有这个 XML 文件:

<questions>
  <question id="title">
    <option>
      <text>Mr</text>
      <value>Mr</value>
    </option>
    <option>
      <text>Ms</text>
      <value>Ms</value>
    </option>
  </question>
  <question id="organisation">
    <option>
      <text>org1</text>
      <value>org1</value>
    </option>
    <option>
      <text>org2</text>
      <value>org2</value>
    </option>
  </question>
</questions>

如何将每个问题绑定到 c# 中的特定下拉列表?

谢谢

【问题讨论】:

    标签: c# .net asp.net xml


    【解决方案1】:

    您可以使用XmlDataSource。因为您的 XML 不符合此控件的预期,您需要使用 XSL 转换对其进行调整。

    所以第 1 步:

    定义一个 XSL 转换 (~/App_Data/questions.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="questions">
        <questions>
          <xsl:apply-templates select="question"/>
        </questions>
      </xsl:template>
      <xsl:template match="option">
        <option>
          <xsl:attribute name="text">
            <xsl:value-of select="text"/>
          </xsl:attribute>
          <xsl:attribute name="value">
            <xsl:value-of select="value"/>
          </xsl:attribute>
        </option>
      </xsl:template>
    </xsl:stylesheet>
    

    第 2 步:

    使用它:

    <%@ Page Language="C#" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    <head id="Head1" runat="server">
        <title></title>
    </head>
    <body>
        <form id="Form1" runat="server">
            <asp:DropDownList 
                ID="ddl" 
                runat="server" 
                DataSourceID="ds" 
                DataTextField="text" 
                DataValueField="value" 
            />
    
            <asp:XmlDataSource 
                ID="ds" 
                runat="server" 
                DataFile="~/App_Data/questions.xml" 
                TransformFile="~/App_Data/questions.xslt" 
                XPath="//option" 
            />
        </form>
    </body>
    </html>
    

    注意数据源上的 TransformFile 属性如何指向 XSL 文件。

    【讨论】:

    • XML 的格式不固定。是否有不需要 XSL 转换的其他格式?
    • 是的,它是由 XSL 转换产生的。如果它未修复,您将需要修复它,否则您将无法使用它。另一种可能性是根据您的 XML 文件的语法动态生成此 XSL 文件,但我真的建议您对其进行规范化。
    • 好的,那么 XPath 将只匹配以下值:问题 id="title"
    • Xpath 将是:/Question[id="title"]/Option。 XmlDataSource 需要注意的一件事是默认情况下启用缓存,这可能会让您有些头疼。我通常将其关闭,即 EnableCache="False"。
    【解决方案2】:

    您可以使用XmlDataSource 类将XML 数据绑定到您的控件。

    【讨论】:

      【解决方案3】:

      你可以使用 XDocument

      string xml = ""; /* your xml */
      XDocument xDocument = XDocument.Parse(xml);
      foreach(XElement questionElement in xDocument.Root.Elements("question"))
      {
       foreach(XElement optionElement in questionElement.Elements("option"))
       {
        string text = optionElement.Element("text").Value;
        string value = optionElement.Element("value").Value;
        /* do something with them here */
       }
      }
      

      使用属性id绑定所有选择框:

      string xml = ""; /* your xml */
      XDocument xDocument = XDocument.Parse(xml);
      foreach(XElement questionElement in xDocument.Root.Elements("question"))
      {
       string id = questionElement.Attribute("id").Value;
       foreach(XElement optionElement in questionElement.Elements("option"))
       {
         string text = optionElement.Element("text").Value;
         string value = optionElement.Element("value").Value;
         /* bind selectbox options here, using id,text,value */
       }
      }
      

      使用id绑定一个选择框:

      string id = "title";  
      string xml = ""; /* your xml */
      XDocument xDocument = XDocument.Parse(xml);  
      XElement questionElement = xDocument.Root.Elements("question").SingleOrDefault(e => e.Attribute("id").Value == "title");
      if (questionElement != null)
      {
       foreach(XElement optionElement in questionElement.Elements("option"))
       {
        string text = optionElement.Element("text").Value;
        string value = optionElement.Element("value").Value;
        /* bind selectbox options here, using id,text,value */
       }
      }
      

      【讨论】:

      • 谢谢。您将如何仅匹配特定问题的节点,即。问题 id="title"?
      • xDocument.Elements("question").Where(e => e.Attribute("id").Value == "title");会这样做。但是,当 id 属性不存在时,您可能必须添加一些异常捕获。还要考虑我发布的新代码。
      • 我喜欢这种方法,但与其循环并将项目添加到列表(每次回发都必须这样做),您可以将列表数据绑定到匿名类型的集合。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多