0

当我尝试从 CUCM AXL api 返回数据时,使用下面的代码

 ExecuteSQLQueryReq query = new ExecuteSQLQueryReq();

        query.sql = "select  * from systables ";

        string[] model = null;
        //get tables

        try
        {       
            executeSQLQueryResponse response = await client.executeSQLQueryAsync(query);

            model = response.executeSQLQueryResponse1.@return.Cast<string>().ToArray();               
        }
        catch (Exception ex)
        {
            Console.WriteLine($"\nError: getQuery: { ex.Message }");
            Environment.Exit(-1);
        }

        Console.WriteLine($"\ngetQuery: SUCCESS  model: { model }\n ");

我得到 sytem.object[] 而不是 sql 数据,我尝试使用下面的代码从每个数据循环

foreach ( string no in model)
{
    Console.WriteLine(no.ToString());
}

我得到的错误是:

无法将“System.Xml.XmlNode[]”类型的对象转换为“System.String”类型。

有没有办法在不来回转换的情况下获取返回的数据

我一直在关注这里的例子

任何帮助,将不胜感激

4

1 回答 1

0

诀窍应该是将 return/rows 转换为“ElementNSImpl”,例如https://github.com/CiscoDevNet/axl-dotnet-samples上的参考示例中所示:

ExecuteSQLQueryReq query = new ExecuteSQLQueryReq();

// Set the text of the SQL query
query.setSql( "select name, pkid from applicationuser" );

// We'll use this list to receive the rows returned by the query
List<Object> user_list = null;

try {
    // Prepare a ExecuteSQLQueryRes object to receive the response from AXL
    ExecuteSQLQueryRes resp = axlPort.executeSQLQuery( query );

    // getRow() returns all of the rows as a List<Object> type
    user_list = resp.getReturn().getRow();

} catch ( Exception e ) {

}

// Create an iterator to cycle through each row, below
Iterator<Object> itr = user_list.iterator();

// While the iterator indicates there is at least one more row...
while ( itr.hasNext() ) {

    // The individual row object is of this ElementNSImpl type - we'll need to cast from generic Object here
    ElementNSImpl el = ( ElementNSImpl )itr.next();

    // Print out the formatted name and pkid values
    System.out.println(
        "Name: " + String.format( "%-20s", el.getElementsByTagName( "name" ).item( 0 ).getTextContent() )+
        " PKID: " + el.getElementsByTagName( "pkid" ).item( 0 ).getTextContent() );
}

虽然请注意该示例基于 Oracle Java 1.8,但使用相同的版本通过 wsimport 从 WSDL 生成代码,并解析结果:

com.sun.org.apache.xerces.internal.dom.ElementNSImpl;
于 2020-06-17T22:04:44.183 回答