Kayra Kayra - 1 month ago 10
C# Question

Change xml element values in c#

i want to change element values for example i want to change cbc: ProfileID, cbc: ID schemeID="VKN">1288331521 < /cbc:ID >, < cbc: StreetName>Papatya Caddesi Yasemin Sokak< /cbc: StreetName>

how can i do that?

this is my xml file

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="general.xslt"?>
<Invoice xsi:schemaLocation="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 ../xsdrt/maindoc/UBL-Invoice-2.1.xsd" xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:n4="http://www.altova.com/samplexml/other-namespace" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2">
<ext:UBLExtensions>
<ext:UBLExtension>
<ext:ExtensionContent>
<n4:auto-generated_for_wildcard/>
</ext:ExtensionContent>
</ext:UBLExtension>
</ext:UBLExtensions>
<cbc:UBLVersionID>2.1</cbc:UBLVersionID>
<cbc:CustomizationID>TR1.2</cbc:CustomizationID>
<cbc:ProfileID>TEMELFATURA</cbc:ProfileID>
<cbc:ID>GIB20090000000001</cbc:ID>
<cbc:CopyIndicator>false</cbc:CopyIndicator>
<cbc:UUID>F47AC10B-58CC-4372-A567-0E02B2C3D479</cbc:UUID>
<cbc:IssueDate>2009-01-05</cbc:IssueDate>
<cbc:IssueTime>14:42:00</cbc:IssueTime>
<cbc:InvoiceTypeCode>SATIS</cbc:InvoiceTypeCode>
<cbc:DocumentCurrencyCode>TRY</cbc:DocumentCurrencyCode>
<cbc:LineCountNumeric>1</cbc:LineCountNumeric>
<cac:InvoicePeriod>
<cbc:StartDate>2008-12-05</cbc:StartDate>
<cbc:EndDate>2009-01-05</cbc:EndDate>
</cac:InvoicePeriod>
<cac:Signature>
<cbc:ID schemeID="VKN_TCKN">1288331521</cbc:ID>
<cac:SignatoryParty>
<cac:PartyIdentification>
<cbc:ID schemeID="VKN">1288331521</cbc:ID>
</cac:PartyIdentification>
<cac:PostalAddress>
<cbc:StreetName>Papatya Caddesi Yasemin Sokak</cbc:StreetName>
<cbc:BuildingNumber>21</cbc:BuildingNumber>
<cbc:CitySubdivisionName>Beşiktaş</cbc:CitySubdivisionName>
<cbc:CityName>İstanbul</cbc:CityName>
<cbc:PostalZone>34100</cbc:PostalZone>
<cac:Country>
<cbc:Name>Türkiye</cbc:Name>
</cac:Country>
</cac:PostalAddress>
</cac:SignatoryParty>
<cac:DigitalSignatureAttachment>
<cac:ExternalReference>
<cbc:URI>#Signature</cbc:URI>
</cac:ExternalReference>
</cac:DigitalSignatureAttachment>
</cac:Signature>
<cac:AccountingSupplierParty>
<cac:Party>
<cbc:WebsiteURI>http://www.aaa.com.tr/</cbc:WebsiteURI>
<cac:PartyIdentification>
<cbc:ID schemeID="VKN">1288331521</cbc:ID>
</cac:PartyIdentification>
<cac:PartyName>
<cbc:Name>AAA Anonim Şirketi</cbc:Name>
</cac:PartyName>
<cac:PostalAddress>
<cbc:ID>1234567890</cbc:ID>
<cbc:StreetName>Papatya Caddesi Yasemin Sokak</cbc:StreetName>
<cbc:BuildingNumber>21</cbc:BuildingNumber>
<cbc:CitySubdivisionName>Beşiktaş</cbc:CitySubdivisionName>
<cbc:CityName>İstanbul</cbc:CityName>
<cbc:PostalZone>34100</cbc:PostalZone>
<cac:Country>
<cbc:Name>Türkiye</cbc:Name>
</cac:Country>
</cac:PostalAddress>
<cac:PartyTaxScheme>
<cac:TaxScheme>
<cbc:Name>Büyük Mükellefler</cbc:Name>
</cac:TaxScheme>
</cac:PartyTaxScheme>
<cac:Contact>
<cbc:Telephone>(212) 925 51515</cbc:Telephone>
<cbc:Telefax>(212) 925505015</cbc:Telefax>
<cbc:ElectronicMail>aa@aaa.com.tr</cbc:ElectronicMail>
</cac:Contact>
</cac:Party>
</cac:AccountingSupplierParty>
<cac:AccountingCustomerParty>
<cac:Party>
<cbc:WebsiteURI/>
<cac:PartyIdentification>
<cbc:ID schemeID="TCKN">1234567890</cbc:ID>
</cac:PartyIdentification>
<cac:PartyIdentification>
<cbc:ID schemeID="TESISATNO">1234567</cbc:ID>
</cac:PartyIdentification>
<cac:PartyIdentification>
<cbc:ID schemeID="SAYACNO">12345678</cbc:ID>
</cac:PartyIdentification>
<cac:PostalAddress>
<cbc:ID>ATATÜRK MAH.</cbc:ID>
<cbc:Room>1</cbc:Room>
<cbc:StreetName>6. Sokak</cbc:StreetName>
<cbc:BuildingNumber>1</cbc:BuildingNumber>
<cbc:CitySubdivisionName>Beşiktaş</cbc:CitySubdivisionName>
<cbc:CityName>İstanbul</cbc:CityName>
<cbc:PostalZone>34100</cbc:PostalZone>
<cac:Country>
<cbc:Name>Türkiye</cbc:Name>
</cac:Country>
</cac:PostalAddress>
<cac:Contact>
<cbc:ElectronicMail>1234567890@mydn.com.tr</cbc:ElectronicMail>
</cac:Contact>
<cac:Person>
<cbc:FirstName>Ali</cbc:FirstName>
<cbc:FamilyName>YILMAZ</cbc:FamilyName>
</cac:Person>
</cac:Party>
</cac:AccountingCustomerParty>
<cac:PaymentTerms>
<cbc:Note>BBB Bank Otomatik Ödeme</cbc:Note>
<cbc:PaymentDueDate>2009-01-20</cbc:PaymentDueDate>
</cac:PaymentTerms>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="TRY">2.73</cbc:TaxAmount>
<cac:TaxSubtotal>
<cbc:TaxableAmount currencyID="TRY">15.15</cbc:TaxableAmount>
<cbc:TaxAmount currencyID="TRY">2.73</cbc:TaxAmount>
<cac:TaxCategory>
<cac:TaxScheme>
<cbc:TaxTypeCode>0015</cbc:TaxTypeCode>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubtotal>
</cac:TaxTotal>
<cac:LegalMonetaryTotal>
<cbc:LineExtensionAmount currencyID="TRY">15.15</cbc:LineExtensionAmount>
<cbc:TaxExclusiveAmount currencyID="TRY">15.15</cbc:TaxExclusiveAmount>
<cbc:TaxInclusiveAmount currencyID="TRY">17.88</cbc:TaxInclusiveAmount>
<cbc:PayableAmount currencyID="TRY">17.88</cbc:PayableAmount>
</cac:LegalMonetaryTotal>
<cac:InvoiceLine>
<cbc:ID>1</cbc:ID>
<cbc:InvoicedQuantity unitCode="KWH">101</cbc:InvoicedQuantity>
<cbc:LineExtensionAmount currencyID="TRY">15.15</cbc:LineExtensionAmount>
<cac:AllowanceCharge>
<cbc:ChargeIndicator>false</cbc:ChargeIndicator>
<cbc:MultiplierFactorNumeric>0.0</cbc:MultiplierFactorNumeric>
<cbc:Amount currencyID="TRY">0</cbc:Amount>
<cbc:BaseAmount currencyID="TRY">15.15</cbc:BaseAmount>
</cac:AllowanceCharge>
<cac:TaxTotal>
<cbc:TaxAmount currencyID="TRY">2.73</cbc:TaxAmount>
<cac:TaxSubtotal>
<cbc:TaxableAmount currencyID="TRY">15.15</cbc:TaxableAmount>
<cbc:TaxAmount currencyID="TRY">2.73</cbc:TaxAmount>
<cbc:Percent>18.0</cbc:Percent>
<cac:TaxCategory>
<cac:TaxScheme>
<cbc:Name>KDV</cbc:Name>
<cbc:TaxTypeCode>0015</cbc:TaxTypeCode>
</cac:TaxScheme>
</cac:TaxCategory>
</cac:TaxSubtotal>
</cac:TaxTotal>
<cac:Item>
<cbc:Name>Elektrik Tüketim Bedeli</cbc:Name>
</cac:Item>
<cac:Price>
<cbc:PriceAmount currencyID="TRY">0.15</cbc:PriceAmount>
</cac:Price>
</cac:InvoiceLine>
</Invoice>

Answer
XDocument doc = XDocument.Load("../../InvoiceSO.xml");
var element = doc.Root.Elements().Where(x => x.Name.LocalName == "ProfileID").FirstOrDefault();

if (element != null)
   element.Value = "YourNewValue";

You are loading your document after that you are searching for specific local name. After that you are putting the desired value.