locked
Extracting Result from XML Nodes RRS feed

  • Question

  • User1216489373 posted

    Hi Experts,

    My XML is as follows:-

                        <Charge Amount="23.00" CurrencyCode="GBP">
                            <Description>
                                <Text>Total cost</Text>
                            </Description>
                        </Charge>
                        <Charge Amount="23.00" CurrencyCode="GBP">
                            <Description>
                                <Text>Adult price</Text>
                            </Description>
                        </Charge>
                        <Charge Amount="17.00" CurrencyCode="GBP">
                            <Description>
                                <Text>Child (age 5 to 15)</Text>
                            </Description>
                        </Charge>

    I want to get result of all nodes, code I am using is:-

                costtype = XmlNodeTour.SelectSingleNode("Charge/Description/Text").InnerText;
                costtype = XmlNodeTour.SelectSingleNode("Charge/Description/Text").InnerText;
                costtype = XmlNodeTour.SelectSingleNode("Charge/Description/Text").InnerText;
    amount = XmlNodeTour.SelectSingleNode("Charge/@Amount").InnerText; amount = XmlNodeTour.SelectSingleNode("Charge/@Amount").InnerText; amount = XmlNodeTour.SelectSingleNode("Charge/@Amount").InnerText;

    But everytime I get result from only first node. That is costtype always shows "Total cost" and amount always shows "23". I am not getting the result from scond and third node i.e. costtype as "Adult price" and "Child (age 5 to 15)" and similarly for amount.

    Please help.

    Thanks,

    Rahul

    Tuesday, March 4, 2014 6:20 AM

Answers

  • User-1516073966 posted

    Hi

    You have to retrieve all the charge nodes and loop through each charge and get the text and amount. Which is as follows:

    XmlNodeList chargeNodes = XmlNodeTour.SelectNodes("Charge");
    
    forech(XmlNode chargeNode in chargeNodes)
    {
       costType = chargetNode.SelectSingleNode("Description/Text").InnerText;
       amount = chargeNode.SelectSingleNode("@Amount").InnerText;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 4, 2014 6:28 AM

All replies

  • User-760709272 posted

    Use SelectNodes rather than SelectSingleNode and that will return an XmlNodeList that contains all the nodes that match the query.  You can iterate through them to process each one.

    http://msdn.microsoft.com/en-us/library/hcebdtae(v=vs.110).aspx

    Tuesday, March 4, 2014 6:28 AM
  • User-1516073966 posted

    Hi

    You have to retrieve all the charge nodes and loop through each charge and get the text and amount. Which is as follows:

    XmlNodeList chargeNodes = XmlNodeTour.SelectNodes("Charge");
    
    forech(XmlNode chargeNode in chargeNodes)
    {
       costType = chargetNode.SelectSingleNode("Description/Text").InnerText;
       amount = chargeNode.SelectSingleNode("@Amount").InnerText;
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, March 4, 2014 6:28 AM
  • User79986525 posted

    Hi ,

    Try Like this 

     public void BindDemoXMLCharge()
        {
            string ConfigFilePath = Server.MapPath("ChangeXML.xml");// "D:/Demo Example/Example1/DemoXMLFile.xml";
    
            EmpRecord objemp = new EmpRecord();
            XDocument xdoc = XDocument.Load(ConfigFilePath);
    
            var employee = (from c in xdoc.Element("Charges").Elements("Charge")
    
                            select new
                            {
    
                                Amount = c.Attribute("Amount").Value,
                                CurrencyCode=c.Attribute("CurrencyCode").Value,
                                Text = c.Element("Description").Element("Text").Value
                               
    
                            }).ToList();
            grid1.DataSource = employee;
            grid1.DataBind();
        }

    For this Your XML should be like this 

    <?xml version="1.0" encoding="utf-8" ?>
    <Charges>
    <Charge Amount="23.00" CurrencyCode="GBP">
      <Description>
        <Text>Total cost</Text>
      </Description>
    </Charge>
    <Charge Amount="23.00" CurrencyCode="GBP">
      <Description>
        <Text>Adult price</Text>
      </Description>
    </Charge>
    <Charge Amount="17.00" CurrencyCode="GBP">
      <Description>
        <Text>Child (age 5 to 15)</Text>
      </Description>
    </Charge>
    </Charges>

    Hope this will help you out 

    Tuesday, March 4, 2014 8:20 AM