从 XML 获取数据:NetSuite WebService

我想从 获取数据NetSuite WebService。这就是我的代码的样子:


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;

    }

}

变量中存在两个节点res。这是第一个节点的样子:


<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>

这就是我的第二个节点的样子:


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

如果响应中存在数据,则它们存在于第二个节点中。如果数据不存在,我想获取第一个节点元素name内的属性值。xs:sequence

吃鸡游戏
浏览 121回答 2
2回答

皈依舞

请尝试这个&nbsp; &nbsp; &nbsp; &nbsp; XmlNode secondNode = getSecondNode();// read the second node from the res&nbsp; &nbsp; &nbsp; &nbsp; if (secondNode != null)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XmlNodeList xnList = secondNode.SelectNodes("//NewDataSet//Departments");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (XmlNode xn in xnList)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string code = xn.SelectSingleNode("Code") != null ? xn.SelectSingleNode("Code").Value : "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string Description = xn.SelectSingleNode("Description") != null ? xn.SelectSingleNode("Description").Value : "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nsmgr.AddNamespace("bk", "http://www.w3.org/2001/XMLSchema");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XmlNode firstNode = getFirstNode();// read the first node from the res&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XmlNodeList xnList = firstNode.SelectNodes("//bk:sequence//bk:element", nsmgr);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (XmlNode xn in xnList)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; string value = ((System.Xml.XmlElement)xn).Attributes["name"].Value;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }

红颜莎娜

尝试以下 xml linq :using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Xml;using System.Xml.Linq;namespace ConsoleApplication122{&nbsp; &nbsp; class Program&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; const string FILENAME = @"c:\temp\test.xml";&nbsp; &nbsp; &nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XDocument doc = XDocument.Load(FILENAME);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XElement root = doc.Root;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XNamespace ns = root.GetDefaultNamespace();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XNamespace nsDiffgr = root.GetNamespaceOfPrefix("diffgr");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; XNamespace nsData = root.GetNamespaceOfPrefix("msdata");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Department> deparments = doc.Descendants(ns + "Departments").Select(x => new Department()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; id = (string)x.Attribute(nsDiffgr + "id"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; code = (string)x.Element(ns + "Code"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; description = (string)x.Element(ns + "Description"),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rowOrder = (int)x.Attribute(nsData + "rowOrder")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }).ToList();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Dictionary<string, Department> dict = deparments&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .GroupBy(x => x.id, y => y)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToDictionary(x => x.Key, y => y.FirstOrDefault());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public class Department&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; public string id { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string code { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public string description { get; set; }&nbsp; &nbsp; &nbsp; &nbsp; public int rowOrder { get;set;}&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP