从多个xml字符串更新节点

时间:2015-11-04 12:34:16

标签: c# xml

我基本上需要将存储在表字段中的数据加密为xml字符串。

我正在使用AES加密。我已经有了加密和解密的逻辑。我想要做的就是加载xml字符串并更新两个节点。

基本上我需要更新

<Bankaccountnumber>23232323</Bankaccountnumber>Banksortcode>232323</Banksortcode>个节点。

我基本上有大约60,000条记录,并希望使用C#程序有效地完成。请参阅下面的xml字符串示例

 <?xml version="1.0" encoding="utf-16"?><callvalidate><authentication><company>Bestinvest Brokers</company><username>API@bestinvest.co.uk</username><password>Pa55word67</password></authentication><sessions><session RID="0cee7f47-59b4-4fb2-a8eb-bafba9dec8ee"><data><Checksrequired><BankStandard>Yes</BankStandard><BankEnhanced>Yes</BankEnhanced><CardLive>No</CardLive><CardEnhanced>No</CardEnhanced><IDEnhanced>Yes</IDEnhanced><DeliveryFraud>No</DeliveryFraud><EmailValidate>No</EmailValidate><CreditScore>No</CreditScore><Zodiac>No</Zodiac><IPAddress>No</IPAddress></Checksrequired><Personalinformation><IndividualDetails><Title>Mr.</Title><Firstname>test</Firstname><Surname>test</Surname><Dateofbirth>1996-02-01T00:00:00</Dateofbirth><Emailaddress>ranjit.menon9@gmail.com</Emailaddress></IndividualDetails><AddressDetails><Buildingname></Buildingname><Postcode>se93qS</Postcode><Previouspostcode /></AddressDetails></Personalinformation><Bankaccountnumber>23232323</Bankaccountnumber><Banksortcode>232323</Banksortcode></data></session></sessions><application>LT-API-BEML</application></callvalidate>

1 个答案:

答案 0 :(得分: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 = @"\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument callvalidate = XDocument.Load(FILENAME);

            List<XElement> bankAccountNumbers = callvalidate.Descendants("Bankaccountnumber").ToList();
            for(int index = 0; index < bankAccountNumbers.Count; index++)
            {
                XElement bankAccountNumber = bankAccountNumbers[index];

                int accountNumber = int.Parse(bankAccountNumber.Value);
                XElement newBankAccountNumber = new XElement("Bankaccountnumber", new object[] {
                    accountNumber,
                    new XElement("Banksortcode",accountNumber)
                });
                bankAccountNumber = newBankAccountNumber;
            }
        }
    }
}
​