如何读取XML文件并显示它和ASPX页面

时间:2017-03-20 07:50:14

标签: javascript jquery html asp.net xml

我有以下xml文件,我希望在点击时以字符串形式显示在aspx页面中。我希望显示Promo 1和promo 2的详细信息(id,from,to,value,promokey)

<POMTOMPROMOTIONS>
<POMTOM>
<POM PerThousandMileCost="40" Currency="USD"/>
<TOM PerThousandMileCost="25" Currency="USD"/>
<POMTOMPromotions>
<Promo Id="Promo1" Transtype="POM" from="25-OCT-2017" to="28-OCT-2020">
<item type="MBNA" value="MBNA" discount="25" promokey="PromoKey1"/>
</Promo>
</POMTOMPromotions>


 <POMTOMTaxes>
      <Tax COR="IN" Taxcode="JN" Percentage="4.94"></Tax>           
    </POMTOMTaxes>
</POMTOM>
<POMTOM>
<POM PerThousandMileCost="70" Currency="USD"/>
<TOM PerThousandMileCost="50" Currency="USD"/>
<POMTOMPromotions>
<Promo Id="Promo2" Transtype="POM" from="04-APR-2017" to="28-JAN-2050">
<item type="CITIBANK" value="CITIBANK" discount="50" promokey="PromoKey2"/>
</Promo>
</POMTOMPromotions>
<POMTOMTaxes>
      <Tax COR="IN" Taxcode="JN" Percentage="4.94"></Tax>           
    </POMTOMTaxes>
</POMTOM>
</POMTOMPROMOTIONS>

2 个答案:

答案 0 :(得分:0)

使用XML解析XMLDocument,获取属性值和子属性值,然后将它们设置在您想要的某个div或任何其他标记内。我已经创建了一个测试代码,可以完美地解决您的问题:

        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(xmlFile);//Load your xmlFile, put path here or xml string
        XmlNodeList nodeList = xmldoc.GetElementsByTagName("Promo");//get all the nodes of Promo
        var nodeX = nodeList.Cast<XmlNode>().
                    Select(x => new
                    {
                        Id = (string)x.Attributes["Id"].Value,
                        from = (string)x.Attributes["from"].Value,
                        to = (string)x.Attributes["to"].Value,
                        value = (string)x.ChildNodes[0].Attributes["value"].Value,
                        promokey = (string)x.ChildNodes[0].Attributes["promokey"].Value
                    });
        foreach (var item in nodeX)
        {   
            //Creating different tags and setting values
            HtmlGenericControl mainDiv = new HtmlGenericControl();
            mainDiv.TagName = "div";

            HtmlGenericControl id = new HtmlGenericControl();
            id.TagName = "h1";
            id.InnerText = item.Id;
            mainDiv.Controls.Add(id);


            HtmlGenericControl from = new HtmlGenericControl();
            from.TagName = "h4";
            from.InnerText = "From: " + item.from;
            mainDiv.Controls.Add(from);

            HtmlGenericControl to = new HtmlGenericControl();
            to.TagName = "h4";
            to.InnerText = "To: " + item.to;
            mainDiv.Controls.Add(to);

            HtmlGenericControl promokey = new HtmlGenericControl();
            promokey.TagName = "h4";
            promokey.InnerText = "promokey: " + item.promokey;
            mainDiv.Controls.Add(promokey);

            HtmlGenericControl value = new HtmlGenericControl();
            value.TagName = "h4";
            value.InnerText = "value: " + item.value;
            mainDiv.Controls.Add(value);

            Panel1.Controls.Add(mainDiv);//Adding all the tags within an asp.net panel
        }

在.aspx方面,我刚刚添加了一个这样的面板:

<asp:Panel ID="Panel1" runat="server"></asp:Panel>

此代码的输出是

Output

希望它有所帮助!

答案 1 :(得分:-1)

XmlTextReader reader = new XmlTextReader(&#34; books.xml&#34;);

看看:

https://support.microsoft.com/en-za/help/307548/how-to-read-xml-from-a-file-by-using-visual-c