UiPath Documentation
activities
latest
false
重要 :
请注意,此内容已使用机器翻译进行了部分本地化。 新发布内容的本地化可能需要 1-2 周的时间才能完成。
UiPath logo, featuring letters U and I in white

开发者活动

上次更新日期 2026年4月21日

读取和反序列化 XML 文件

以下示例说明了如何使用“反序列化 XML”“执行 XPath”“获取 XML 节点”“获取 XML 节点属性”等活动来读取、反序列化、访问和显示有关 XML 文件的各种信息。您可以在“UiPath.WebAPI.Activities ”包中找到这些活动。

备注:

Download and extract the archive with the project in this example and copy the file daily_usd.xml to your project folder.

此自动化流程的构建方法如下:

  1. 打开 Studio ,创建一个新“流程”
  2. 将“序列”容器拖动至“工作流设计器”
    • 创建以下变量:

      变量名称变量类型默认值
      xml字符串
      xmlDeserializedXDocument
      xml_xpath_result对象
      xmlNodesIEnumerable
      xmlAttributesIEnumerable
      allExchRates字符串
  3. Add a Read Text File activity inside the Sequence.
    • Add the expression "daily_usd.xml" in the File Name field.
    • In the Properties panel, add the variable xml in the Content field.
  4. “反序列化 XML”活动添加到“读取文本文件”活动之后。
    • Add the variable xml in the XML String field.
    • In the Properties panel, add the variable xmlDeserialized in the XMLDocument.
  5. “执行 XPath”活动添加到“反序列化 XML”活动之后。
    • Add the variable xmlDeserialized in the Existing XML field.
    • Add the expression "string(/channel/lastBuildDate)" in the XPath Expression field.
    • In the Properties panel, add the variable xml_xpath_result in the Result field.
  6. “执行 XPath”活动之后添加“消息框”活动。
    • 在“文本”字段中添加表达式 "Exchange rates for " + xml_xpath_result.ToString
  7. “获取 XML 节点”活动添加到“消息框”活动之后。
    • Add the variable xmlDeserialized in the ExistingXML field.
    • In the Properties panel, add the variable xmlNodes in the XMLNodes field.
  8. 将“获取 XML 节点属性”活动添加到“获取 XML 节点”活动之后。
    • Add the expression xmlNodes(0) in the Existing XML Node field.
    • In the Properties panel, add the variable xmlAttributes in the Attributes field.
  9. “获取 XML 节点属性”活动之后添加“消息框”活动。
    • 在“文本”字段中添加表达式 xmlAttributes(0).Name.Tostring + ": " + xmlAttributes(0).Value.Tostring
  10. “分配”活动添加到“消息框”活动之后。
    • 在“收件人”字段中添加“allExchRates”变量。
    • 在“值”字段添加表达式 "Exhange Rates" + System.Environment.NewLine
  11. 将“遍历循环”活动添加到“分配”活动之后。
    • Add the expression xmlDeserialized.Element("channel").Elements("item") in the Values field.
    • In the Properties panel, select the System.Xml.Linq.XElement option from the TypeArgument drop-down list.
  12. Add an Assign activity inside the Sequence container of the For Each activity.
    • 在“收件人”字段中添加“allExchRates”变量。
    • “值”字段中添加以下表达式:`allExchRates + System.Environment.NewLine + "1 " + item.Element("baseName").Value.ToString + " = " + item.Element("exchangeRate").Value。目标字符串
      • + item.Element("targetName").Value.ToString`。
  13. “消息框”活动添加到“遍历循环”活动之后。
    • 在“文本”字段中添加“allExchRates”变量。

      文档图像

  14. 运行流程。 系统将读取、反序列化和访问 XML 文件。 系统将显示几个消息框,其中包含从文件中提取的信息。

您可在此处下载示例。

此页面有帮助吗?

连接

需要帮助? 支持

想要了解详细内容? UiPath Academy

有问题? UiPath 论坛

保持更新