首页 > 解决方案 > 转换文件根据子节点的文本值替换整个父节点 - XML Transform

问题描述

我需要识别子节点元素的文本值。

目的:

我想为 XML 文件创建转换文件,所以我需要通过比较子节点ClientKey的值来替换父节点Connection

我的 XML 文件:

<Connection>
     <ClientKey>Client1</ClientKey>
     <ConnectionString>Test</ConnectionString>
     <WorkingDocs>Test</WorkingDocs>
     <TemplateDocs>Test</TemplateDocs>
     <PatientDocs>Test</PatientDocs>
     <ClientName>Test</ClientName>
     <ClientTimeZone>Test</ClientTimeZone>
     <ClientTimeDiff>Test</ClientTimeDiff>
     <ExceptionLogPath>Test</ExceptionLogPath>
</Connection>

我如何识别ClientKey节点的文本

标签: xmlxpathtransformxml-document-transform

解决方案


了解 LINQ to XML 可能会有更多用处。请查看https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/concepts/linq/linq-to-xml-overview

XML 值可以存储在“元素”或“属性”下。考虑下面的 xml 文件。

<Connection>
 <ClientKey Name = "KeyName" >Client1</ClientKey>
 <ConnectionString>Test</ConnectionString>
 <WorkingDocs>Test</WorkingDocs>
 <TemplateDocs>Test</TemplateDocs>
 <PatientDocs>Test</PatientDocs>
 <ClientName>Test</ClientName>
 <ClientTimeZone>Test</ClientTimeZone>
 <ClientTimeDiff>Test</ClientTimeDiff>
 <ExceptionLogPath>Test</ExceptionLogPath>

XElement Data = XElement.Load() //你的 Xml 参考在这里

  1. 获取元素值:(客户端键是元素

字符串结果 = data.Element("ClientKey").value

  1. 获取 Attribute 值:(Attribute 是 ClientKey 中的 Name 属性

字符串结果 = data.Attribute("Name").value


推荐阅读