-3

我有下面给出的 XML。在这个 XML 中,在一个带有将包含数据的 ASCX 控件的 ASPX 页面中,我想要一个带有选项“GMAT”、“GRE”、“LSAT”和“MCAT”的下拉菜单。当我在下面选择 GMAT 时,它只会显示 GMAT 小册子,与其他小册子相同。

您能否为此提供一些代码?

<?xml version="1.0" encoding="utf-8"?>
<root>
    <data>
        <ImageId>tcm556662</ImageId>
        <ImageURL>/Images/2008_gmat_brochure_uk_thumb_tcm55-7311.gif</ImageURL>
        <ImageTitle>GMAT Brochure</ImageTitle>
        <Description>&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;Classroom
                     and Online Coursegrams.&lt;/p&gt;</Description>
        <FilePath>/Images/gmat_brochure_uk_tcm55-8064.pdf</FilePath>
    </data>
    <data>
        <ImageId>tcm5510981</ImageId>
        <ImageURL>/Images/practice-test-image_tcm55-10980.JPG</ImageURL>
        <ImageTitle>GMAT Problem Solving Questions and Answers</ImageTitle>
        <Description>Download Kaplan's GMAT Problem So to each question.</Description>
        <FilePath>/Images/gmat-sample-questions_tcm55-10979.pdf</FilePath>
    </data>
    <data>
        <ImageId>tcm5511066</ImageId>
        <ImageURL>/Images/practice-test-image_tcm55-10980.JPG</ImageURL>
        <ImageTitle>GMAT Sentence Correction Practice Questions</ImageTitle>
        <Description>&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;Tick the box to
                     download GMsee how you might do on this section of the GMAT
                     exam.&lt;/p&gt;</Description>
        <FilePath>/Images/gmat-sentence-correction_tcm55-11065.pdf</FilePath>
    </data>
    <data>
        <ImageId>tcm556663</ImageId>
        <ImageURL>/Images/gre_brochure_thumb_tcm55-7315.gif</ImageURL>
        <ImageTitle>GRE Brochure</ImageTitle>
        <Description>&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;Courses and
                     Tutoring for the Grprograms&lt;/p&gt;</Description>
        <FilePath>/Images/gre-brochure_tcm55-8065.pdf</FilePath>
    </data>
    <data>
        <ImageId>tcm5511219</ImageId>
        <ImageURL>/Images/practice-test-image_tcm55-10980.JPG</ImageURL>
        <ImageTitle>GRE Quantitative Questions Answers and Explanations</ImageTitle>
        <Description>Download the answers and exp with the correct answer. Tick
                    the box and click download now to start the process.</Description>
        <FilePath>/Images/gre-quantitative-practice-questions_tcm55-11214.pdf</FilePath>
    </data>
    <data>
        <ImageId>tcm5511220</ImageId>
        <ImageURL>/Images/practice-test-image_tcm55-10980.JPG</ImageURL>
        <ImageTitle>GRE Sentence Completion Practice Question Answers and Explanations</ImageTitle>
        <Description>&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;Complete the
                    sentence on the download now button to see how you did.&lt;/p&gt;</Description>
        <FilePath>/Images/gre-sentence-completions_tcm55-11213.pdf</FilePath>
    </data>
    <data>
        <ImageId>tcm558073</ImageId>
        <ImageURL>/Images/lsat_brochure_thumb_tcm55-7316.gif</ImageURL>
        <ImageTitle>LSAT Brochure</ImageTitle>
        <Description>Courses and Tutoring for the Law School Admission US law schools</Description>
        <FilePath>/Images/lsat-brochure_tcm55-8066.pdf</FilePath>
    </data>
    <data>
        <ImageId>tcm5511275</ImageId>
        <ImageURL>/Images/practice-test-image_tcm55-10980.JPG</ImageURL>
        <ImageTitle>LSAT Comparative Reading Practice Questions Answers and Explanations</ImageTitle>
        <Description>Try answering the LSAT Comparative Reading and click on
                     download now.</Description>
        <FilePath>/Images/comparative-reasoning_tcm55-11269.pdf</FilePath>
    </data>
    <data>
        <ImageId>tcm5511281</ImageId>
        <ImageURL>/Images/practice-test-image_tcm55-10980.JPG</ImageURL>
        <ImageTitle>LSAT Reading Comprehension Practice Questions Answers and Explanations</ImageTitle>
        <Description>&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;Read the passage
                     and try the questions as now Lion Practice Questions Answers
                     and Explanations.&lt;/p&gt;</Description>
        <FilePath>/Images/lsat-reading-comprehension_tcm55-11280.pdf</FilePath>
    </data>
    <data>
        <ImageId>tcm558074</ImageId>
        <ImageURL>/Images/mcat_brochure_thumb_tcm55-7317.gif</ImageURL>
        <ImageTitle>MCAT Brochure</ImageTitle>
        <Description>Courses and Tutoring for the Medical College Admission Test
                     (MCAT)</Description>
        <FilePath>/Images/mcat-brochure_tcm55-8067.pdf</FilePath>
    </data>
    <data>
        <ImageId>tcm5511278</ImageId>
        <ImageURL>/Images/practice-test-image_tcm55-10980.JPG</ImageURL>
        <ImageTitle>MCAT Biological Sciences Practice Questions Answers and Explanations</ImageTitle>
        <Description>Have you wri download now to find out how you've done.</Description>
        <FilePath>/Images/mcat-biological-sciences_tcm55-11271.pdf</FilePath>
    </data>
</root>

下面是读取 XML 文件的函数:

private DataSet GenerateDatasetFromXml()
    {
        string xmlFile = string.Empty;
        DataSet xmlFileData = new DataSet();
        string selItem = string.Empty;
        xmlFile = @Server.MapPath("~/" + ConfigurationManager.AppSettings["BrochureXml"].ToString());
        string sfinalString = "";
        hdnIds.Value = "";
        try
        {
            if (File.Exists(xmlFile))
            {
                xmlFileData.ReadXml(xmlFile);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        } 
        return xmlFileData; 
    }
4

3 回答 3

1

如果您有可用的 LINQ,您可以执行以下操作...我不确定 ImageTitle 元素是否是您的匹配元素,但是...

XDocument xml = XDocument.Load("MyXMLFile.xml");

string selectedItem = "GMAT"; //Wherever you get this string from

var matchedItems = xml.Root.Descendants("data")
                           .Where(ele => ele.Element("ImageTitle").Value.StartsWith(selectedItem));
于 2009-04-03T04:53:41.010 回答
1

我想要一个带有选项“GMAT”、“GRE”、“LSAT”、“MCAT”的下拉列表

现在,您只能根据字符串解析 ImageTitle 并在其中找到子字符串来选择这些项目 - 不是最优的......

如果有任何机会,我建议在您的数据中添加一个标签(或任何您想调用的标签),如下所示:

<data>
  <ImageId>tcm556662</ImageId>
  <ImageURL>/Images/2008_gmat_brochure_uk_thumb_tcm55-7311.gif</ImageURL>
  <ImageTitle>GMAT Brochure</ImageTitle>
  <Type>GMAT</Type>
  <Description>&lt;p xmlns="http://www.w3.org/1999/xhtml"&gt;Classroom and Online Coursegrams.&lt;/p&gt;</Description>
  <FilePath>/Images/gmat_brochure_uk_tcm55-8064.pdf</FilePath>
</data>

这样,选择合适的元素就像一个非常简单的 XPath 表达式一样简单:

XmlNodeList _list = myXmlDocument.SelectNodes("//data[Type = 'GMAT']);

一旦您将数据文件加载到 XmlDocument 中(或者如果您愿意,您可以使用 Linq-to-XML 方式)。

马克

于 2009-04-03T05:33:45.390 回答
0

查看您拥有的 XML 结构,无需以最简单的方式进行格式化即可开箱即用。

如果你有能力改变结构,我会添加一个“”或类似的节点,甚至可能是元素上的一个属性。在那里,您可以对每个项目进行分组,提供您想要的价值。

然后使用 XML 操作方法,您可以轻松地使用 XPath 从列表中选择项目,或创建可用项目的列表。

于 2009-04-03T04:35:28.900 回答