You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在XML文件中为单个键写入多个值?

实现XML字典中一个键对应多个值

好问题!要让你的XML字典支持一个键存储多个值,你需要调整现有结构——因为原来的设计里每个<Item>的<Value>只容纳单个<string>元素。这里有两种常用的规范实现方式,你可以根据自己的解析逻辑或需求来选:

方案1:让<Value>包含多个<string>元素

这种方式最符合“一个键对应一组值”的语义,把单个的<string>替换成多个,放在同一个<Value>节点下:

<?xml version="1.0" encoding="utf-16"?>
<dictionary>
  <Item>
    <Key>
      <string>Stations</string>
    </Key>
    <Value>
      <string>Station 1</string>
    </Value>
  </Item>
  <Item>
    <Key>
      <string>Vendors</string>
    </Key>
    <Value>
      <string>Vendor 1</string>
      <string>Vendor 2</string>
    </Value>
  </Item>
</dictionary>

方案2:添加重复的<Item>元素(同一个Key对应多条条目)

如果你的解析逻辑允许,也可以保留原来的单值结构,为同一个Vendors键新增一个完整的<Item>节点:

<?xml version="1.0" encoding="utf-16"?>
<dictionary>
  <Item>
    <Key>
      <string>Stations</string>
    </Key>
    <Value>
      <string>Station 1</string>
    </Value>
  </Item>
  <Item>
    <Key>
      <string>Vendors</string>
    </Key>
    <Value>
      <string>Vendor 1</string>
    </Value>
  </Item>
  <Item>
    <Key>
      <string>Vendors</string>
    </Key>
    <Value>
      <string>Vendor 2</string>
    </Value>
  </Item>
</dictionary>

小提示

  • 如果这个XML是给特定的程序/解析器使用的,最好先确认它支持哪种格式——比如有些解析器默认只读取每个Key的第一个值,这时候方案1会更可靠。
  • 方案1的结构更清晰,适合需要明确“一组值”的场景;方案2更简单,适合快速扩展且解析逻辑能处理重复Key的情况。

内容的提问来源于stack exchange,提问作者user8166711

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 09:43:19