-4

I have the following xml file and i want to be displayed in aspx page as string on click . I want Promo 1 and promo 2 details ( id ,from, to , value , promokey) to be displayed

<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 Answers2

0

Parse XML using XMLDocument, get the attribute value and child attribute value and then set them within some div or anyother tag you want. I've created a test code which perfectlly addresses your problem:

        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
        }

In the .aspx side, I just added a panel like this:

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

The output of this code is this

Output

Hope it helps!

M. Adeel Khalid
  • 1,786
  • 2
  • 21
  • 24
-1

XmlTextReader reader = new XmlTextReader ("books.xml");

Have a look:

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