Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..

my string Contains data in xml formatt
For example:


XML
<relatedCategory>
<count>351</count>
<name>Computer-Wholesale & Manufacturers</name>
</relatedCategory>
<relatedCategory>
<count>278</count>
<name>Computer Software & Services</name>
</relatedCategory>
<relatedCategory>
<count>271</count>
<name>Computer Software Publishers & Developers</name>
</relatedCategory>



i want to extract values from this like

351
Computer-Wholesale & Manufacturers

i tried some code but it is not getting those values.it is showing just count,name.

Thanks in advance...
Posted

C#
string xml =      "<relatedcategory>"
                + "<count>351</count>"
                + "<name>Computer-Wholesale & Manufacturers</name>"
                + "</relatedcategory>"
                + "<relatedcategory>"
                + "<count>278</count>"
                + "<name>Computer Software & Services</name>"
                + "</relatedcategory>"
                + "<relatedcategory>"
                + "<count>271</count>"
                + "<name>Computer Software Publishers & Developers</name>"
                + "</relatedcategory>";

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml("<root>" + xml.Replace("&", "amp;") + "</root>");

XmlNode xNode = xdoc.DocumentElement.FirstChild;

string item = xNode.InnerText;//your item
 
Share this answer
 
You can use XElement from .NET 3.5.

C#
XElement xmlRoot = XElement.Parse(xmlString);

var results = from r in xmlRoot.Elements("relatedCategory")
              select new { Count = r.Element("count").Value, Name = r.Element("name").Value };


And then for-each all results.
C#
foreach (var result in results)
{
  Console.WriteLine("Count: {0}, Name: {1}", result.Count, result.Name);
}


It will produce an out like:

Count: 351, Name: Computer-Wholesale & Manufacturers
 
Share this answer
 
v2
HTML
HttpWebRequest webRequestObject = null;
       StreamReader sr = null;
       HttpWebResponse webResponseObject = null;
       webRequestObject = (HttpWebRequest)WebRequest.Create("API from where i am getting XML Data");
       webRequestObject.Method = "GET";

       webRequestObject.UserAgent = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.0.3705)";

       webResponseObject = (HttpWebResponse)webRequestObject.GetResponse();
       sr = new StreamReader(webResponseObject.GetResponseStream());

       String Results = sr.ReadToEnd();
       using (XmlReader reader = XmlReader.Create(new StringReader(Results)))
       {
           // Parse the file and display each of the nodes.
           while (reader.Read())
           {
               if (reader.Name == "businessName" && reader.NodeType == XmlNodeType.Element)
               {
                   // Advace to the element's text node

                 Response.Write(reader.ReadInnerXml());

                   // ... do what you want (you can get the value with reader.Value)
               }
            }
          }
 
Share this answer
 
C#
DataSet ds = new DataSet();
ds.ReadXml(new StringReader(yourxmlstring));
 
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