Dot Net developer
Dot Net developer

Reputation: 436

getting datas from XML : NetSuite WebService

I want to get data from NetSuite WebService. This is how my code looks like:

public class HomeController : Controller
{
    WebServiceGenesisSoapClient service;
    public HomeController()
    {
        var aa = WebServiceGenesisSoapClient.EndpointConfiguration.WebServiceGenesisSoap;
        service = new WebServiceGenesisSoapClient(aa);
    }

    public async Task<IActionResult> Index()
    {
        WebServiceGenesisSoapClient client = new WebServiceGenesisSoapClient(aa, "http://82.71.28.125/WebServiceGenesis/WebServiceGenesis.asmx");

        GetStockQuantityArrayRequest getStockQuantityArrayRequest = new GetStockQuantityArrayRequest();
        var stocklist = client.GetStockQuantityArrayAsync(getStockQuantityArrayRequest).Result;
        ArrayOfXElement res = stocklist.GetStockQuantityArrayResult;
    }
}

Two Nodes are present in res variable. This is how first node looks like:

<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="StockQuantityArray">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Colour" type="xs:string" minOccurs="0" />
              <xs:element name="Quantity1" type="xs:int" minOccurs="0" />
              <xs:element name="Quantity2" type="xs:int" minOccurs="0" />
              <xs:element name="Quantity3" type="xs:int" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

This is how my second node looks like:

<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1" />

If datas are present in response, they are present in second node. If datas are not present, I want to get value of name attribute inside xs:sequence element of first node.

EDIT 1: If datas are present , then first node looks like:

<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
  <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
    <xs:complexType>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="Departments">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="Code" type="xs:string" minOccurs="0" />
              <xs:element name="Description" type="xs:string" minOccurs="0" />
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:choice>
    </xs:complexType>
  </xs:element>
</xs:schema>

and second node looks like:

<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
  <NewDataSet xmlns="">
    <Departments diffgr:id="Departments1" msdata:rowOrder="0">
      <Code>ALL</Code>
      <Description>ALL</Description>
    </Departments>
    <Departments diffgr:id="Departments2" msdata:rowOrder="1">
      <Code>BABY</Code>
      <Description>BABY</Description>
    </Departments>
    <Departments diffgr:id="Departments3" msdata:rowOrder="2">
      <Code>CHIL</Code>
      <Description>CHILD</Description>
    </Departments>
    <Departments diffgr:id="Departments4" msdata:rowOrder="3">
      <Code>TEEN</Code>
      <Description>TEEN</Description>
    </Departments>
    <Departments diffgr:id="Departments5" msdata:rowOrder="4">
      <Code>WM</Code>
      <Description>WOMAN</Description>
    </Departments>
  </NewDataSet>
</diffgr:diffgram>

Upvotes: 1

Views: 324

Answers (2)

Sain Pradeep
Sain Pradeep

Reputation: 3125

Please try this

        XmlNode secondNode = getSecondNode();// read the second node from the res
        if (secondNode != null)
        {
            XmlNodeList xnList = secondNode.SelectNodes("//NewDataSet//Departments");
            foreach (XmlNode xn in xnList)
            {
                string code = xn.SelectSingleNode("Code") != null ? xn.SelectSingleNode("Code").Value : "";
                string Description = xn.SelectSingleNode("Description") != null ? xn.SelectSingleNode("Description").Value : "";
            }
        }
        else
        {

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
            nsmgr.AddNamespace("bk", "http://www.w3.org/2001/XMLSchema");
            XmlNode firstNode = getFirstNode();// read the first node from the res
            XmlNodeList xnList = firstNode.SelectNodes("//bk:sequence//bk:element", nsmgr);
            foreach (XmlNode xn in xnList)
            {
                string value = ((System.Xml.XmlElement)xn).Attributes["name"].Value;
            }
        }

Upvotes: 1

jdweng
jdweng

Reputation: 34421

Try following xml linq :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication122
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement root = doc.Root;
            XNamespace ns = root.GetDefaultNamespace();
            XNamespace nsDiffgr = root.GetNamespaceOfPrefix("diffgr");
            XNamespace nsData = root.GetNamespaceOfPrefix("msdata");

            List<Department> deparments = doc.Descendants(ns + "Departments").Select(x => new Department()
            {
                id = (string)x.Attribute(nsDiffgr + "id"),
                code = (string)x.Element(ns + "Code"),
                description = (string)x.Element(ns + "Description"),
                rowOrder = (int)x.Attribute(nsData + "rowOrder")
            }).ToList();

            Dictionary<string, Department> dict = deparments
                .GroupBy(x => x.id, y => y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());
        }
    }
    public class Department
    {
        public string id { get; set; }
        public string code { get; set; }
        public string description { get; set; }
        public int rowOrder { get;set;}
    }

}

Upvotes: 0

Related Questions