1
using System;
using System.Xml;
using SampleWebserviceApp.net.myasp.deepakgaur;
namespace SampleWebserviceApp
{
 class Program
 {
    static void Main(string[] args)
    {
        InventoryWebservice Webservice = new InventoryWebservice();
        var value = Webservice.GetDataByID("1");
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(value);
        string id = doc.GetElementsByTagName("Id").ToString();
        Console.WriteLine(value);
        Console.WriteLine(id);
        Console.ReadKey();
       }
    }
}

can anyone help me out how can i get node vaue from my web service response

<NewDataSet>
  <Table>
    <Id>1         </Id>
    <ProductName>Hp Laptop</ProductName>
    <SerialNumber>abc111    </SerialNumber>
    <Status>Delivered           </Status>
    <CustomerName>Shatakshi</CustomerName>
  </Table>
</NewDataSet>

what i am trying to do is get id , productname, serial number, status , cusomername value in respective varialble
plaese help me understand how can i achive this.

1
  • What code did you try? Commented Aug 4, 2015 at 7:08

1 Answer 1

1

Use Linq to XML. Please note that I don't have the web service InventoryWebservice, so I just create an xml document manually.

using System;
using System.IO;
using System.Linq;
using System.Xml.Linq;

class Program
{
    static void Main()
    {
        // create an XML document manually
        var doc = XDocument.Load(new StringReader(@"<NewDataSet>
  <Table>
    <Id>1         </Id>
    <ProductName>Hp Laptop</ProductName>
    <SerialNumber>abc111    </SerialNumber>
    <Status>Delivered           </Status>
    <CustomerName>Shatakshi</CustomerName>
  </Table>
</NewDataSet>"));

        // query the XML document
        var product = doc
            .Descendants("Table")
            .Select(x => new {
                        Id = x.Element("Id").Value,
                        ProductName = x.Element("ProductName").Value,
                        SerialNumber = x.Element("SerialNumber").Value,
                        Status = x.Element("Status").Value,
                        CustomerName = x.Element("CustomerName").Value
                        })
            .FirstOrDefault();

        // print the result
        if (product != null)
        {
            Console.WriteLine("Id = {0}", product.Id);
            Console.WriteLine("Product Name = {0}", product.ProductName);
            Console.WriteLine("Serial Number = {0}", product.SerialNumber);
            Console.WriteLine("Status = {0}", product.Status);
            Console.WriteLine("Customer Name = {0}", product.CustomerName);
        }

        Console.Write("Press any key to continue . . . ");
        Console.ReadKey(true);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Handoko Chen it works just fine, really appreciate your fast response thanks again

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.