创建新话题
已回答

Reading Attribute with SID: DTNV (Name Value) from API

How can I read the Named value list with the APIWhen I try it and convert it to string I got a lot of Chinese signs. How is it encoded? 


最佳答案

Here is an example of an attribute that should work the same way as yours. First a picture on how it looks in the client:



The below code will read the attribute. The data will be in xml-format, so you need to parse the data yourself if you want to display it.

In this example, I am replacing "Value2" with "Value3" and writing it back. Notice that you need ZCompressStringToBytes() if you want to you write back.

This code will display the xml below


            SystemWeaverAPI.SWConnection.Instance.LoginName = "test";
            SystemWeaverAPI.SWConnection.Instance.Password = "test";
            SystemWeaverAPI.SWConnection.Instance.ServerMachineName = "localhost";
            SystemWeaverAPI.SWConnection.Instance.ServerPort = int.Parse("1769");

            SystemWeaverAPI.SWConnection.Instance.Login();

            var testItem = SWConnection.Instance.Broker.GetItem(SWHandleUtility.ToHandle("x040000000008711F"));

            var attr = testItem.Attribute("DTNV");

            var xmlStr = attr.ValueAsXML;
            Console.Write(xmlStr);
            xmlStr = xmlStr.Replace("Value2", "Value3");

            testItem.SetAttributeDataWithSID("DTNV", SystemWeaver.Common.Internal.SWCompression.ZCompressStringToBytes(xmlStr));



Xml read from the attribute:

<NamedValues>
  <NamedValue>
    <Min>1</Min>
    <Max>1</Max>
    <Name>Value1</Name>
  </NamedValue>
  <NamedValue>
    <Min>2</Min>
    <Max>2</Max>
    <Name>Value2</Name>
  </NamedValue>
</NamedValues>


Attribute after code has replaced "Value2" with "Value3":

1 Comment

回答

Here is an example of an attribute that should work the same way as yours. First a picture on how it looks in the client:



The below code will read the attribute. The data will be in xml-format, so you need to parse the data yourself if you want to display it.

In this example, I am replacing "Value2" with "Value3" and writing it back. Notice that you need ZCompressStringToBytes() if you want to you write back.

This code will display the xml below


            SystemWeaverAPI.SWConnection.Instance.LoginName = "test";
            SystemWeaverAPI.SWConnection.Instance.Password = "test";
            SystemWeaverAPI.SWConnection.Instance.ServerMachineName = "localhost";
            SystemWeaverAPI.SWConnection.Instance.ServerPort = int.Parse("1769");

            SystemWeaverAPI.SWConnection.Instance.Login();

            var testItem = SWConnection.Instance.Broker.GetItem(SWHandleUtility.ToHandle("x040000000008711F"));

            var attr = testItem.Attribute("DTNV");

            var xmlStr = attr.ValueAsXML;
            Console.Write(xmlStr);
            xmlStr = xmlStr.Replace("Value2", "Value3");

            testItem.SetAttributeDataWithSID("DTNV", SystemWeaver.Common.Internal.SWCompression.ZCompressStringToBytes(xmlStr));



Xml read from the attribute:

<NamedValues>
  <NamedValue>
    <Min>1</Min>
    <Max>1</Max>
    <Name>Value1</Name>
  </NamedValue>
  <NamedValue>
    <Min>2</Min>
    <Max>2</Max>
    <Name>Value2</Name>
  </NamedValue>
</NamedValues>


Attribute after code has replaced "Value2" with "Value3":

登录 发表评论