在c#中更改XML元素值

时间:2016-02-16 10:02:54

标签: c# xml

我的C#代码:

XDocument doc = XDocument.Load(filename); 
IEnumerable<XElement> collection = 
    doc.Elements("BCIRequest").Elements("Card").Elements("SelectedPIN");

我的XML文档:

<?xml version="1.0" encoding="utf-8"?>
 <BCIRequest Version="2.0"
        xmlns="urn:xxxxxx:bci:request">
  <Header>
<SenderCode>XX99</SenderCode>
<SenderID>9999</SenderID>
<SequenceNumber>123</SequenceNumber>
<CardGroupCount>2</CardGroupCount>
<CardCount>4</CardCount>
<BlockCount>2</BlockCount>
 </Header>
      <!--card groups (must precede cards and blocks)-->
       <CardGroup RequestID="1">
        <CustomerNumber>XX01234567</CustomerNumber>
        <CardGroupName Emboss="true">GROUP ONE</CardGroupName>
  </CardGroup>

  <CardGroup RequestID="2"
            RequestRef="87416CB7-DAEF-483A-BD08-1A885531D958">
   <CustomerNumber>XX12345678</CustomerNumber>
<CardGroupName Emboss="false">GROUP TWO</CardGroupName>
 </CardGroup>
  <Card RequestID="3">
<CustomerNumber>XX01234567</CustomerNumber>
<DriverCard>
  <Driver Emboss="true">MARGE SIMPSON</Driver>
</DriverCard>
<CardTypeID>10</CardTypeID>
<PurchaseCategoryID>11</PurchaseCategoryID>
<Reissue>false</Reissue>
<GeneratedPIN/>
<OdoPrompt>false</OdoPrompt>
<CRNPrompt>false</CRNPrompt>
 </Card>
  <Card RequestID="4">
<CustomerNumber>XX12345678</CustomerNumber>
<VehicleCard>
  <VRN Emboss="true">KYI 830</VRN>
</VehicleCard>
<CardTypeID>10</CardTypeID>
<PurchaseCategoryID>11</PurchaseCategoryID>
<Reissue>false</Reissue>
<SelectedPIN>0123</SelectedPIN>
<OdoPrompt>false</OdoPrompt>
<CRNPrompt>false</CRNPrompt>
  </Card>
  <Card RequestID="5">
<CustomerNumber>XX01234567</CustomerNumber>
<BearerCard>
  <Bearer Emboss="true">OPEN XXXXXX</Bearer>
</BearerCard>
<CardTypeID>10</CardTypeID>
<PurchaseCategoryID>11</PurchaseCategoryID>
<Reissue>false</Reissue>
<FleetPIN/>
<OdoPrompt>false</OdoPrompt>
<CRNPrompt>false</CRNPrompt>
  </Card>
   <Block RequestID="6">
<CustomerNumber>XX01234567</CustomerNumber>
<PAN>7002999999999999991</PAN>
  </Block>
  <Card RequestID="7"
    RequestRef="956EA6C5-7D7E-4622-94D0-38CAD9FCC8DF">
<CustomerNumber>XX01234567</CustomerNumber>
<DriverCard>
  <Driver Emboss="true">HOMER SIMPSON</Driver>
  <VRN Emboss="true">795 DVI</VRN>
</DriverCard>
<EmbossText>SPRINGFIELD POWER</EmbossText>
<CardTypeID>10</CardTypeID>
<TokenTypeID>20</TokenTypeID>
<PurchaseCategoryID>30</PurchaseCategoryID>
<ExpiryDate>2018-12</ExpiryDate>
<Reissue>true</Reissue>
<SelectedPIN>0123</SelectedPIN>
<OdoPrompt>true</OdoPrompt>
<CRNPrompt>true</CRNPrompt>
<!--address with optional fields specified-->
<CardDeliveryAddress OneTimeUse="false">
  <ContactName>M xxxx</ContactName>
  <ContactTitle>Mr</ContactTitle>
  <CompanyName>Sxxxx</CompanyName>
  <Line1>Sector 22-F</Line1>
  <Line2>Springfield Power Plant</Line2>
  <Line3>xxx Road</Line3>
  <City>xxxx</City>
  <Zipcode>xxxx</Zipcode>
  <CountryCode>xxx</CountryCode>
</CardDeliveryAddress>
<!--address with only required fields-->
<PINDeliveryAddress OneTimeUse="true">
  <Line1>xxxx</Line1>
  <City>xxx</City>
  <Zipcode>xxxx</Zipcode>
  <CountryCode>xxxx</CountryCode>
</PINDeliveryAddress>
<Limits>
  <Value Transaction="unlimited" Daily="200" Weekly="unlimited" Monthly="400"/>
  <Volume Transaction="100" Daily="unlimited" Weekly="unlimited" Monthly="unlimited"/>
  <Transactions Daily="unlimited" Weekly="unlimited" Monthly="unlimited"/>
  <Day Monday="true" Tuesday="true" Wednesday="true" Thursday="true" Friday="true" Saturday="false" Sunday="false"/>
  <Time Start="unlimited" End="17:00:00"/>
</Limits>
<Products>
  <FuelProductRestrictionID>40</FuelProductRestrictionID>
  <NonFuelProductRestrictionID>51</NonFuelProductRestrictionID>
  <NonFuelProductRestrictionID>52</NonFuelProductRestrictionID>
  <NonFuelProductRestrictionID>53</NonFuelProductRestrictionID>
  <NonFuelProductRestrictionID>54</NonFuelProductRestrictionID>
  <NonFuelProductRestrictionID>55</NonFuelProductRestrictionID>
</Products>
  </Card>

  <Block RequestID="8"
     RequestRef="69A3E44D-DC10-4BEE-9249-1FC3C651BA0E">
<CustomerNumber>xxxxx</CustomerNumber>
<PAN>xxxxxx</PAN>
  </Block>
 </BCIRequest>

我需要更新上述值中的元素值。旧的价值是:

<SelectedPIN>0123</SelectedPIN>

新值应为:

<SelectedPIN EncryptedPIN="TKDS" FormNumber="000793906306">****</SelectedPIN>

任何人都可以帮我这个吗? 如果我选择了BCIRequest元素,它将返回一个空值。我尝试了很多解决方案但无法让这个XML文件工作。

2 个答案:

答案 0 :(得分:3)

可以通过多种方式修改Xml,我更喜欢XDocument

XDocument doc = XDocument.Parse(input);

foreach (var element in  doc.Descendants("SelectedPIN")) // filter if you want single element, in example I modifed for all elements.
{
    element.Add(new XAttribute("EncryptedPIN", "TKDS"));
    element.Add(new XAttribute("FormNumber", "000793906306"));
    element.Value = "xxxxx"; //new value
}

最后您可以使用

保存文档
doc.Save();

看看这个Demo

答案 1 :(得分:0)

根节点(BCIRequest)包含命名空间,因此您需要将其包含在查询中。这样的事情应该有效:

        XNamespace ns = "urn:xxxxxx:bci:request";
        IEnumerable<XElement> collection = doc.Elements(ns + "BCIRequest").Elements(ns + "Card").Elements(ns + "SelectedPIN");