首页 > 解决方案 > XML 批量更改类型十六进制

问题描述

所以我有 80 000 行 XML。它们遵循以下一般结构:

   <Object type="0xa14" id="Steel Dagger">
  <Class>Equipment</Class>
  <Item/>
  <Texture>
     <File>lofiObj5</File>
     <Index>0x60</Index>
  </Texture>
  <SlotType>2</SlotType>
  <Tier>0</Tier>
  <Description>{equip.A_sharp_dagger_made_of_steel.}</Description>
  <RateOfFire>1</RateOfFire>
  <Sound>weapon/blunt_dagger</Sound>
  <Projectile>
     <ObjectId>Blade</ObjectId>
     <Speed>140</Speed>
     <MinDamage>20</MinDamage>
     <MaxDamage>60</MaxDamage>
     <LifetimeMS>400</LifetimeMS>
  </Projectile>
  <BagType>1</BagType>
  <OldSound>daggerSwing</OldSound>
  <feedPower>5</feedPower>
  <DisplayId>{equip.Steel_Dagger}</DisplayId>

但我想做的是改变 XML 的所有类型。

类型是这部分:

type="0xa14"

我希望它们从 0x00 开头(第一个 XML)开始,然后以十六进制增加 1 直到它到达结尾。

这是 XML 文件的纯 pastebin: XML Github File

标签: c#xml

解决方案


您可以使用 XDocument 并更新type循环中的所有属性

var document = XDocument.Load(@"pathToFile");
var index = 1;
foreach (var ground in document.Descendants("Ground"))
{
    ground.Attribute("type").Value = index.ToString("X"); 
    // 'X' convert integer to hexadecimal representation
    index++;
}

document.Save(@"pathToUpdatedFile");

推荐阅读