XML批量更改类型十六进制

时间:2018-06-30 05:05:35

标签: c# 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

2 个答案:

答案 0 :(得分:0)

您可以使用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");

答案 1 :(得分:0)

使用xml linq简单:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);

            int index = 0;
            foreach(XElement obj in doc.Descendants("Object"))
            {
                obj.SetAttributeValue("type", "0x" + index++.ToString("x"));
            }
        }
    }
}