xml - 转换文件根据子节点的文本值替换整个父节点 - 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节点的文本
解决方案
了解 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 参考在这里
- 获取元素值:(客户端键是元素)
字符串结果 = data.Element("ClientKey").value
- 获取 Attribute 值:(Attribute 是 ClientKey 中的 Name 属性)
字符串结果 = data.Attribute("Name").value
推荐阅读
- sql - 根据条件 postgresql 查找先前的值
- amazon-web-services - 通过 AWS 云在 Kubernetes 中运行 windows 容器
- java - 图片和视频不显示在页面中
- c# - 无法激活 Windows 应用商店应用。激活请求失败,出现错误“访问被拒绝”
- sparql - SPARQL - 从字符串中获取实体类型
- java - Java Collator 排序
- excel - 自 Office 365 升级后 VBA 中的 MailItem.Send 无法正常工作
- java - 创建给定 Class 类型的实例
- r - 使用自写的包部署闪亮的应用程序
- git - 是否可以在每个 repo 的基础上为 NPM 添加代理设置?