如何根据用户选择在 ASPX 中显示 XML 数据?

发布于 2024-07-16 19:20:07 字数 6508 浏览 6 评论 0原文

我有下面给出的 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; 
    }

I have the XML given below. From this XML, in an ASPX page with an ASCX control that will contain the data, I want a drop-down on top with option "GMAT", "GRE", "LSAT", and "MCAT". And when I select GMAT below it will show only GMAT brochure and same with others.

Can you please provide some code for this?

<?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><p xmlns="http://www.w3.org/1999/xhtml">Classroom
                     and Online Coursegrams.</p></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><p xmlns="http://www.w3.org/1999/xhtml">Tick the box to
                     download GMsee how you might do on this section of the GMAT
                     exam.</p></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><p xmlns="http://www.w3.org/1999/xhtml">Courses and
                     Tutoring for the Grprograms</p></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><p xmlns="http://www.w3.org/1999/xhtml">Complete the
                    sentence on the download now button to see how you did.</p></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><p xmlns="http://www.w3.org/1999/xhtml">Read the passage
                     and try the questions as now Lion Practice Questions Answers
                     and Explanations.</p></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>

Below is the Function for reading the XML file:

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; 
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

彼岸花ソ最美的依靠 2024-07-23 19:20:07

我想要顶部有一个包含选项“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><p xmlns="http://www.w3.org/1999/xhtml">Classroom and Online Coursegrams.</p></Description>
  <FilePath>/Images/gmat_brochure_uk_tcm55-8064.pdf</FilePath>
</data>

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

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

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

马克

I want a dropdown on top with option "GMAT", "GRE", "LSAT", "MCAT"

Right now, you could only select those items based on string-parsing the ImageTitle and finding substring in it - less than optimal...

If there's any chance at all, I'd recommend adding a tag (or whatever you want to call it) to your data, something like this:

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

That way, selecting the proper elements would be as easy a very simple XPath expression:

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

once you have loaded your data file into an XmlDocument (or you the Linq-to-XML way, if you prefer that).

Marc

不再让梦枯萎 2024-07-23 19:20:07

如果您有可用的 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));

If you have LINQ available you could do the following... I'm not sure if the ImageTitle element is your matching element but...

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));
向地狱狂奔 2024-07-23 19:20:07

查看那里的 XML 结构,无需以最简单的方式进行格式化即可开箱即用地执行此操作。

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

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

Looking at the XML structure that you have there it is not necessary formatted in the easiest way to be able to do this out of the box.

If you have the ability to change the structure I would add a "" or similar node, or maybe even an attribute on the element. In there you can group each of your items, providing your desired value.

Then using XML manipulation methods you can easily use XPath to select items out of the list, or to create a listing of available items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文