两个常用的Infopath Service读取域值的函数

本文提供两个实用方法:通过XPath设置节点值及获取节点内XML内容。利用XPathNavigator和XmlNamespaceManager进行高效定位与操作。

//设置域值 

   public static string NavSetValue(XPathNavigator xn, string strXPath,XmlNamespaceManager xnm,string strValue)
    {
        try
        {
            if (xn != null)
            {
                xn.SelectSingleNode(strXPath, xnm).SetValue(strValue);
                return xn.Value;
            }
            else
            {
                return "";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }

    }

  

//读域值   

 public static string GetInnerXML(XPathNavigator xMain, XmlNamespaceManager xnm, string strXPath)
    {
        try
        {
            if (xMain != null)
            {
                System.Xml.XmlNamespaceManager xNameSpace = new System.Xml.XmlNamespaceManager(new System.Xml.NameTable());
                xNameSpace.AddNamespace("my", xnm.LookupNamespace("my").ToString());
                System.Xml.XPath.XPathNavigator FieldNavn = xMain.SelectSingleNode(strXPath, xNameSpace);

                return FieldNavn.InnerXml;
            }
            else
            {
                return "";
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

### 实现ESP32作为蓝牙客户端读取特定服务下两个特征 为了实现ESP32作为一个蓝牙低功耗(BLE)客户端来读取来自另一个BLE设备的服务中的两个特征,可以遵循以下方法。此过程涉及初始化BLE库、扫描目标设备、连接到它以及随后访问所需的服务和特征。 #### 初始化BLE环境 在`setup()`函数中完成必要的初始化工作: ```cpp #include <Arduino.h> #include <BLEDevice.h> #include <BLEUtils.h> #include <BLEScan.h> #include <BLEAdvertisedDevice.h> class MyClientCallback : public BLECharacteristicCallbacks { void onRead(BLECharacteristic *pChar) override { Serial.print("Value Read: "); Serial.println(pChar->getValue().c_str()); } }; void setup() { Serial.begin(115200); // Initialize the device. BLEDevice::init(""); // Create the client class which will manage operations with the server we are connecting to. pClient = BLEDevice::createClient(); } ``` 上述代码展示了如何启动BLE功能并创建一个用于管理与远程服务器交互的客户端实例[^1]。 #### 连接到指定设备和服务 一旦建立了基本框架,则需定位具体的目标设备及其提供的服务: ```cpp // Connects to a specific advertised device based on its name or address. bool connectToServer(std::stringservername){ Serial.printf("Forming a connection to %s\n",servername.c_str()); // Start scanning for devices... BLEScan* pBLEScan = BLEDevice::getScan(); // Get scanner instance. BLEScanResults foundDevices; while(true){ foundDevices = pBLEScan->start(3,true); // Scan duration is set at 3 seconds here. for(int i=0; i<foundDevices.getCount();i++){ BLEAdvertisedDevice advertisedDevice = foundDevices.getDevice(i); if(strcmp(servername.c_str(),advertisedDevice.getName().c_str())==0){ Serial.println("Found our target device!"); // Attempt to establish a connection. bool success = pClient->connect(&advertisedDevice); if(success){ return true; }else{ Serial.println("Failed to connect."); } break; } } } } void loop(){ if(!connected && !connecting){ connected = connectToServer("Target Device Name"); if(connected){ // Once successfully connected, proceed to discover services and characteristics. BLERemoteService* pRemoteService = pClient->getService(serviceUUID); if (pRemoteService != nullptr) { // Retrieve both characteristics within this service. BLERemoteCharacteristic* pRemoteCharacteristicOne = pRemoteService->getCharacteristic(characteristicUUID_one); BLERemoteCharacteristic* pRemoteCharacteristicTwo = pRemoteService->getCharacteristic(characteristicUUID_two); if ((pRemoteCharacteristicOne != nullptr)&&(pRemoteCharacteristicTwo!=nullptr)) { // Set up callbacks so that when these values change they can be processed automatically. pRemoteCharacteristicOne->registerForNotify(new MyClientCallback()); pRemoteCharacteristicTwo->registerForNotify(new MyClientCallback()); // Perform an initial read operation after setting everything up. std::string valueOne = pRemoteCharacteristicOne->readValue(); std::string valueTwo = pRemoteCharacteristicTwo->readValue(); Serial.print("Initial Value One:"); Serial.println(valueOne.c_str()); Serial.print("Initial Value Two:"); Serial.println(valueTwo.c_str()); } } } } } ``` 这段程序说明了怎样通过名称匹配找到想要连接的对象,并尝试建立链接。成功之后会获取对应的服务对象,再从中提取出所需的两个特征对象。最后注册通知机制以便实时监听数据变化的同时也进行了初次的数据读取操作[^2]。 #### 定义常量变量 确保提前声明好要用到的一些全局变量比如服务ID(`serviceUUID`)还有各个特征项各自的ID(`characteristicUUID_one`, `characteristicUUID_two`)等,在实际应用里这些通常是由API文档提供或者是事先协商好的固定字符串形式表示。 ```cpp #define SERVICE_UUID "your-service-uuid-here" #define CHARACTERISTIC_UUID_ONE "first-characteristic-id" #define CHARACTERISTIC_UUID_TWO "second-characteristic-id" static const char* TARGET_DEVICE_NAME = "Your Target Device"; BLERemoteCharacteristic* pRemoteCharacteristicOne; BLERemoteCharacteristic* pRemoteCharacteristicTwo; boolean connected=false; boolean connecting=false; BLEClient* pClient; ``` 以上就是
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值