XML 到 Gridview 如果可能吗?

发布于 2024-11-19 20:55:17 字数 2833 浏览 1 评论 0原文

美好的一天,我将尝试尽可能简短但信息丰富,我使用的是 asp.net 3.5 和 C#,基本上我想做的是从我从 HTTPRequest 收到的 xml 文件中收集信息。但是,使用通常的 dataset.readxml 对我来说不起作用。在发布任何答案之前,请花一点时间查看所有内容。

我收到的 xml 看起来像这样:(我已经删除了一些信息,但很高兴提供我收到的完整 XML 集,因为我试图不用垃圾信息发送垃圾邮件)

    <SEGMENTS>
<SEGMENT NAME="webcluster">
    <RESULTPAGE>
        <QUERYTRANSFORMS>
            <QUERYTRANSFORM NAME="Original query" ACTION="NOP" QUERY="printers" CUSTOM="" MESSAGE="Original query" MESSAGEID="1"/>
            <QUERYTRANSFORM NAME="FastQT_Lemmatizer" ACTION="nop" QUERY="" CUSTOM="No change to query" MESSAGE="Lemmatization turned off for current query" MESSAGEID="16" INSTANCE="lemmatizer"/>
            <QUERYTRANSFORM NAME="Final query" ACTION="NOP" QUERY="string("printers")" CUSTOM="FQL" MESSAGE="Final query" MESSAGEID="1"/>
        </QUERYTRANSFORMS>
        <NAVIGATION ENTRIES="0">
        </NAVIGATION>
        <CLUSTERS/>
        <RESULTSET FIRSTHIT="1" LASTHIT="1" HITS="1" TOTALHITS="1121" MAXRANK="6610" TIME="0.0000">
            <HIT NO="1" RANK="6610" SITEID="0" MOREHITS="0" FCOCOUNT="0">
                <FIELD NAME="body"/>
                <FIELD NAME="customerid">36547986</FIELD>
                <FIELD NAME="name">Yahunn Digital <key>Printers</key></FIELD>
                <FIELD NAME="itemcode">DTRJ</FIELD>
                <FIELD NAME="bookcode">155512</FIELD>
                <FIELD NAME="heading">BANNERS</FIELD>
                <FIELD NAME="link2">15530694.GIF</FIELD>
            </HIT>
        </RESULTSET>
        <PAGENAVIGATION>
            <NEXTPAGE FIRSTHIT="2" LASTHIT="2" URL="/cgi-bin/xml-search?query=printers&hits=1&offset=1"/>
        </PAGENAVIGATION>
    </RESULTPAGE>
</SEGMENT>

我尝试使用以下内容来阅读它:

    HttpWebRequest request = WebRequest.Create(builder.Uri) as HttpWebRequest;
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            // Get the response stream  
            StreamReader readerSR = new StreamReader(response.GetResponseStream());

            // Console application output  
            //lblReturn.Text = readerSR.ReadToEnd().ToString();
            DataSet XMLDataSet = new DataSet();
            XMLDataSet.ReadXml(readerSR);
            dgvResults.DataSource = XMLDataSet.Tables[9].DefaultView;
            dgvResults.DataBind();
        }; 

但简单的答案是,这不会返回一组正确的信息来填充网格视图。表 XMLDataSet.Tables[7].Rows.Count.ToString() 包含 223 行,这是完全不正确的,我给出的 xml 是 1 个结果(当然,我遗漏了示例中返回的 216 个“字段”)但它似乎正在为每个“FIELD”创建一行。我的问题是,我如何将信息获取到 DataGridView 中,或者我应该做一些完全不同的事情来显示此信息?我想使用gridview的原因纯粹是因为我对gridview的定制感到满意。

Good day, I am going to try be as breif but informative as possible, I am using asp.net 3.5 and C#, basically what I am attempting to do is collect information from a xml file I recieve from a HTTPRequest. However using the usual dataset.readxml is not working for me. Please take a minute to look through everything before posting any answers.

I recieve xml that looks like this: (I have removed some information but am happy to supply the full XML set that I recieve as I am trying not to spam this with rubbish info)

    <SEGMENTS>
<SEGMENT NAME="webcluster">
    <RESULTPAGE>
        <QUERYTRANSFORMS>
            <QUERYTRANSFORM NAME="Original query" ACTION="NOP" QUERY="printers" CUSTOM="" MESSAGE="Original query" MESSAGEID="1"/>
            <QUERYTRANSFORM NAME="FastQT_Lemmatizer" ACTION="nop" QUERY="" CUSTOM="No change to query" MESSAGE="Lemmatization turned off for current query" MESSAGEID="16" INSTANCE="lemmatizer"/>
            <QUERYTRANSFORM NAME="Final query" ACTION="NOP" QUERY="string("printers")" CUSTOM="FQL" MESSAGE="Final query" MESSAGEID="1"/>
        </QUERYTRANSFORMS>
        <NAVIGATION ENTRIES="0">
        </NAVIGATION>
        <CLUSTERS/>
        <RESULTSET FIRSTHIT="1" LASTHIT="1" HITS="1" TOTALHITS="1121" MAXRANK="6610" TIME="0.0000">
            <HIT NO="1" RANK="6610" SITEID="0" MOREHITS="0" FCOCOUNT="0">
                <FIELD NAME="body"/>
                <FIELD NAME="customerid">36547986</FIELD>
                <FIELD NAME="name">Yahunn Digital <key>Printers</key></FIELD>
                <FIELD NAME="itemcode">DTRJ</FIELD>
                <FIELD NAME="bookcode">155512</FIELD>
                <FIELD NAME="heading">BANNERS</FIELD>
                <FIELD NAME="link2">15530694.GIF</FIELD>
            </HIT>
        </RESULTSET>
        <PAGENAVIGATION>
            <NEXTPAGE FIRSTHIT="2" LASTHIT="2" URL="/cgi-bin/xml-search?query=printers&hits=1&offset=1"/>
        </PAGENAVIGATION>
    </RESULTPAGE>
</SEGMENT>

I attempt to read it using the following:

    HttpWebRequest request = WebRequest.Create(builder.Uri) as HttpWebRequest;
        using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
        {
            // Get the response stream  
            StreamReader readerSR = new StreamReader(response.GetResponseStream());

            // Console application output  
            //lblReturn.Text = readerSR.ReadToEnd().ToString();
            DataSet XMLDataSet = new DataSet();
            XMLDataSet.ReadXml(readerSR);
            dgvResults.DataSource = XMLDataSet.Tables[9].DefaultView;
            dgvResults.DataBind();
        }; 

But the simple answer is that this does not return a proper set of information to populate the gridview with. The table XMLDataSet.Tables[7].Rows.Count.ToString() contains 223 rows, which is completely incorrect, with the xml I gave that is 1 result(ofcourse I left out 216 "fields" which are returned for the example) but it seems it is creating a row for each "FIELD". My question is, how would I either get the information into a DataGridView, or should I be doing something enirely different to display this information? The reason I would like to use a gridview is purely because I am confrtable with the customization of a gridview.

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

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

发布评论

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

评论(1

婴鹅 2024-11-26 20:55:17

您的 XML 架构错误,因此无法读取 XML 文件
所以出现了无法加载XML的问题。
请尝试在渲染到网格视图之前修复 xml 架构

QUERY="string("printers")" => QUERY="string('printers')"

请注意 '&'破坏 XML 并导致渲染错误,因此我用 %26% 替换它。

以及根标签 [SEGMENTS] 的关闭标签

希望这会对您有所帮助

You have a wrong XML schema so you cant read the XML file
so the problem that you cant load the XML.
please try to fix xml schema befor render to grid view

QUERY="string("printers")" => QUERY="string('printers')"

Note that the '&' destroy the XML and cause a render error so i used to replace it by %26%.

and the close tag for the root tag [SEGMENTS]

hope this will help you

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