【问题标题】:getting datas from XML : NetSuite WebService从 XML 获取数据:NetSuite WebService
【发布时间】:2019-12-06 09:37:36
【问题描述】:

我想从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" />

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

编辑 1: 如果存在数据,则第一个节点如下所示:

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

第二个节点看起来像:

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

【问题讨论】:

    标签: c# xml asp.net-mvc web-services netsuite


    【解决方案1】:

    请试试这个

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

    【讨论】:

    • 第一个 xml 是一个不包含任何数据的模式。架构只列出了可以在第二个 xml 中返回的可能对象。我需要查看带有实际数据响应的第二个文件的一些示例才能给出一个好的答案。
    • 这是问题的最后一行“如果数据不存在,我想获取第一个节点的 xs:sequence 元素内的名称属性值。”所以这可能有助于从第一个节点获取值。
    • @jdweng 先生,我已经编辑了我的问题,以便在数据存在时呈现数据格式。
    【解决方案2】:

    尝试以下 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;}
        }
    
    }
    

    【讨论】:

    • 先生,如果数据不存在,我需要获取元素的名称
    • 如何获得不存在的东西的名称?您可以列出所有数据类型,然后检查该项目是否在列表中。
    • 如果数据不存在,那么响应将采用与问题中提到的相同的格式。
    • 第一个 xml 在模式中具有属性:minOccurs="0",这意味着该属性可以为空。正如我所说,找到丢失对象的唯一方法是创建所有属性的列表,然后根据收到的列表测试可能的属性列表。看起来您发布的两个架构来自不同类型的响应。第一个来自“StockQuantityArray”,第二个来自“Departments”(与发布的 xml 相同)。为什么会得到不同的架构格式?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多