2016-11-02 32 views
-1

我想從我的xmldocument中獲取節點,並且想要更改它,最後我想在我的xmldocument.i中添加新的invoiceline,以便再次添加發票行。我怎樣才能做到這一點?可能嗎?對不起,我的英語不好。從xml中獲取節點並添加xmldocument

這是我的xml文件

<?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>[email protected]</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>[email protected]</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> 

我想這樣做。

<?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>[email protected]</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>[email protected]</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> 
<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> 
+0

是的,它是可能的。看[這裏](http://stackoverflow.com/a/18509248/60761)的一個開始。你可能想看看處理命名空間的其他答案。但之前都有人問過。 –

+0

如果你想更容易嘗試[this](https://github.com/631320085/XmlObject)。 – Bucketcode

回答

0

使用XML的LINQ

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

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

      XElement invoice = (XElement)doc.Descendants().Where(x => x.Name.LocalName == "Invoice").FirstOrDefault(); 

      XNamespace cacNs = invoice.GetNamespaceOfPrefix("cac"); 
      XElement newInvoice = new XElement(cacNs + "InvoiceLine"); 
      invoice.Add(newInvoice); 
     } 
    } 
} 
+0

我很抱歉我的英文不好。我想複製所有的發票,並再次添加XML。感謝您的幫助 – Kayra

+0

你需要一個新的XML或只是添加到現有的?我的代碼添加到現有的XML。 – jdweng

+0

你能檢查我的問題嗎? – Kayra