Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
Hi..
 
my string Contains data in xml formatt
For example:
 

<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 11-Sep-12 20:18pm
074051.1K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You can use XElement from .NET 3.5.
 
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.
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
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

 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)
                }
             }
           }
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

DataSet ds = new DataSet();
ds.ReadXml(new StringReader(yourxmlstring));
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 554
1 Gihan Liyanage 338
2 RyanDev 268
3 Sergey Alexandrovich Kryukov 263
4 ChauhanAjay 220
0 Sergey Alexandrovich Kryukov 8,936
1 OriginalGriff 7,891
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 12 Sep 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100