Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
<ReferralMessages>
                <message>Declined/Obsolete/Imported Vehicles are not Insured Online, Please contact 022-39898282 or any of our branches for more information </message>

                <message>You will not be able to buy this policy online, as the Age of the vehicle is more than the permissible limit.  Please contact  or any of our branches for more information.</message>

        </ReferralMessages>


how can i read Second column from XML to dataset if column name are same.
Posted
Comments
CHill60 27-Jan-15 3:07am    
There are several ways to achieve this - can you post the code that you have to get as far as reading the first message node so that any solutions are properly focussed on what you already have

You could extract the messages like this:
C#
string xml = @"<referralmessages>
  <message>Declined/Obsolete/Imported Vehicles are not Insured Online, Please contact 022-39898282 or any of our branches for more information </message> 
  <message>You will not be able to buy this policy online, as the Age of the vehicle is more than the permissible limit.  Please contact  or any of our branches for more information.</message> 
  </referralmessages>";

XDocument xDocument = XDocument.Parse(xml);

IEnumerable<XElement> xElements = xDocument.Descendants("message");

foreach(XElement xElement in xElements)
{
    string message = xElement.Value;
}
 
Share this answer
 
v2
Comments
John C Rayan 27-Jan-15 11:32am    
If you need only 2nd element then you can use predicate in your xpath like /message[pos=2]. Try xpathnavigator in .net
In the interests of completeness here are two alternatives to using XDocument to access your xml string
C#
using System.Xml;
using System.IO;
...
            var x = XmlReader.Create(new StringReader(xml));
            while (x.Read())
                Console.WriteLine(x.Value);

or
using System.Xml;
            var xmlDoc = new XmlDocument();
            xmlDoc.LoadXml(xml);
            foreach (XmlElement x in xmlDoc.GetElementsByTagName("message"))
                Console.WriteLine(x.InnerText);

The method you choose will depend upon how you want to search the document (back and forth, straight through, in blocks etc.) and obviously on what you have already done (which is why I asked in the comment).

There is a discussion on the 3 methods given in these Solutions (1 and 2) here[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900