EMDI는 지금도 개발중

C# : 전자세금계산서 전자서명을 뺀 나머지 기본정보 XML로 생성하기(2) 본문

IT/전자세금계산서

C# : 전자세금계산서 전자서명을 뺀 나머지 기본정보 XML로 생성하기(2)

EMDI 2020. 4. 6. 15:53

그 다음은 3단계인 전자세금계산서 기본정보를 생성하는 방법에 대해 알아보도록 하겠습니다.

한국인터넷진흥원에서 제공한 전자세금계산서의 전자문서 구조를 보시면 항목 총 5가지로 나눠져 있는데요. 관리정보, 전자서명, 기본정보, 거래처정보, 상품정보가 있니다. 물론 해당 항목 속에는 세부항목으로 나눠지는 부분이 또 있기 때문에 자세한 내용은 한국인터넷진흥원의 개발지침 변경안을 확인해주세요.

 

1. 하드코딩으로 XML 파일 만들기

public void getXml()
{
    XmlDocument xdoc = new XmlDocument();

    xdoc.AppendChild(xdoc.CreateXmlDeclaration("1.0", "UTF-8", ""));

    //===================================================================
    // 루트노드
    XmlNode root = xdoc.CreateElement("TaxInvoice");

    XmlAttribute xsi = xdoc.CreateAttribute("xsi:schemaLocation");
    xsi.Value = "urn:kr:or:kec:standard:Tax:ReusableAggregateBusinessInformationEntitySchemaModule:1:0 http://www.kec.or.kr/standard/Tax/TaxInvoiceSchemaModule_1.0.xsd";
    root.Attributes.Append(xsi);

    XmlAttribute xmlns = xdoc.CreateAttribute("xmlns");
    xmlns.Value = "urn:kr:or:kec:standard:Tax:ReusableAggregateBusinessInformationEntitySchemaModule:1:0";
    root.Attributes.Append(xmlns);

    XmlAttribute xmlnsxsi = xdoc.CreateAttribute("xmlns:xsi");
    xmlnsxsi.Value = "http://www.w3.org/2001/XMLSchema-instance";
    root.Attributes.Append(xmlnsxsi);

    xdoc.AppendChild(root);

    //===================================================================
    // 1. ExchangedDocument
    //===================================================================
    XmlNode exchangedDocument = xdoc.CreateElement("ExchangedDocument");
    XmlNode exchangedDocument_ID = xdoc.CreateElement("ID");
    exchangedDocument_ID.InnerText = "201208064100000100000001";
    exchangedDocument.AppendChild(exchangedDocument_ID);

    XmlNode issueDt = xdoc.CreateElement("IssueDateTime");
    issueDt.InnerText = "20120806174653";
    exchangedDocument.AppendChild(issueDt);

    XmlNode refDoc = xdoc.CreateElement("ReferencedDocument");
    XmlNode refDoc_attr = xdoc.CreateElement("ID");
    refDoc_attr.InnerText = "201208064100000100000001";
    refDoc.AppendChild(refDoc_attr);
    exchangedDocument.AppendChild(refDoc);

    //------------------------------------------------------------------
    root.AppendChild(exchangedDocument);

    //===================================================================
    // 2. TaxInvoiceDocument
    //===================================================================

    XmlNode taxInvoiceDocument = xdoc.CreateElement("TaxInvoiceDocument");
    XmlNode IssueID = xdoc.CreateElement("IssueID");
    IssueID.InnerText = "201208064100000100000001";
    taxInvoiceDocument.AppendChild(IssueID);

    XmlNode TypeCode = xdoc.CreateElement("TypeCode");
    TypeCode.InnerText = "0101";
    taxInvoiceDocument.AppendChild(TypeCode);

    XmlNode IssueDateTime = xdoc.CreateElement("IssueDateTime");
    IssueDateTime.InnerText = "20091005";
    taxInvoiceDocument.AppendChild(IssueDateTime);

    XmlNode PurposeCode = xdoc.CreateElement("PurposeCode");
    PurposeCode.InnerText = "01";
    taxInvoiceDocument.AppendChild(PurposeCode);

    //------------------------------------------------------------------
    root.AppendChild(taxInvoiceDocument);

    //===================================================================
    // 3. TaxInvoiceTradeSettlement
    //===================================================================
    XmlNode TaxInvoiceTradeSettlement = xdoc.CreateElement("TaxInvoiceTradeSettlement");

    //===================================================================
    // 3-1 InvoicerParty
    //===================================================================
    XmlNode InvoicerParty = xdoc.CreateElement("InvoicerParty");

    XmlNode InvoicerParty_ID = xdoc.CreateElement("ID");
    InvoicerParty_ID.InnerText = "2158757426";
    InvoicerParty.AppendChild(InvoicerParty_ID);

    XmlNode TypeCode1 = xdoc.CreateElement("TypeCode");
    TypeCode1.InnerText = "서비스";
    InvoicerParty.AppendChild(TypeCode1);

    XmlNode NameText1 = xdoc.CreateElement("NameText");
    NameText1.InnerText = "(주)헬로월드";
    InvoicerParty.AppendChild(NameText1);

    XmlNode ClassificationCode1 = xdoc.CreateElement("ClassificationCode");
    ClassificationCode1.InnerText = "정보처리";
    InvoicerParty.AppendChild(ClassificationCode1);

    XmlNode SpecifiedPerson1 = xdoc.CreateElement("SpecifiedPerson");
    XmlNode NameText2 = xdoc.CreateElement("NameText");
    NameText2.InnerText = "대표자명";
    SpecifiedPerson1.AppendChild(NameText2);
    InvoicerParty.AppendChild(SpecifiedPerson1);

    XmlNode DefinedContact = xdoc.CreateElement("DefinedContact");
    XmlNode DepartmentNameText = xdoc.CreateElement("DepartmentNameText");
    DepartmentNameText.InnerText = "담당부서";
    DefinedContact.AppendChild(DepartmentNameText);

    XmlNode PersonNameText = xdoc.CreateElement("PersonNameText");
    PersonNameText.InnerText = "담당자이름";
    DefinedContact.AppendChild(PersonNameText);

    XmlNode TelephoneCommunication = xdoc.CreateElement("TelephoneCommunication");
    TelephoneCommunication.InnerText = "전화번호";
    DefinedContact.AppendChild(TelephoneCommunication);

    XmlNode URICommunication = xdoc.CreateElement("URICommunication");
    URICommunication.InnerText = "전자우편";
    DefinedContact.AppendChild(URICommunication);
    InvoicerParty.AppendChild(DefinedContact);

    XmlNode SpecifiedAddress = xdoc.CreateElement("SpecifiedAddress");
    XmlNode LineOneText = xdoc.CreateElement("LineOneText");
    LineOneText.InnerText = "서울시 강남구 삼성동";
    SpecifiedAddress.AppendChild(LineOneText);
    InvoicerParty.AppendChild(SpecifiedAddress);

    //------------------------------------------------------------------
    TaxInvoiceTradeSettlement.AppendChild(InvoicerParty);
    //===================================================================


    //===================================================================
    // 3-2 InvoiceeParty
    //===================================================================
    XmlNode InvoiceeParty = xdoc.CreateElement("InvoiceeParty");

    XmlNode InvoiceeParty_ID = xdoc.CreateElement("ID");
    InvoiceeParty_ID.InnerText = "2158757426";
    InvoiceeParty.AppendChild(InvoiceeParty_ID);

    XmlNode TypeCode2 = xdoc.CreateElement("TypeCode");
    TypeCode2.InnerText = "서비스";
    InvoiceeParty.AppendChild(TypeCode2);

    XmlNode NameText3 = xdoc.CreateElement("NameText");
    NameText3.InnerText = "(주)헬로월드";
    InvoiceeParty.AppendChild(NameText3);

    XmlNode ClassificationCode2 = xdoc.CreateElement("ClassificationCode");
    ClassificationCode2.InnerText = "정보처리";
    InvoiceeParty.AppendChild(ClassificationCode2);

    XmlNode SpecifiedOrganization = xdoc.CreateElement("SpecifiedOrganization");
    XmlNode BusinessTypeCode = xdoc.CreateElement("BusinessTypeCode");
    BusinessTypeCode.InnerText = "01";
    SpecifiedOrganization.AppendChild(BusinessTypeCode);
    InvoiceeParty.AppendChild(SpecifiedOrganization);

    XmlNode SpecifiedPerson2 = xdoc.CreateElement("SpecifiedPerson");
    XmlNode NameText4 = xdoc.CreateElement("NameText");
    NameText4.InnerText = "대표자명";
    SpecifiedPerson2.AppendChild(NameText4);
    InvoiceeParty.AppendChild(SpecifiedPerson2);

    XmlNode PrimaryDefinedContact = xdoc.CreateElement("PrimaryDefinedContact");
    XmlNode DepartmentNameText1 = xdoc.CreateElement("DepartmentNameText");
    DepartmentNameText1.InnerText = "담당부서";
    PrimaryDefinedContact.AppendChild(DepartmentNameText1);

    XmlNode PersonNameText1 = xdoc.CreateElement("PersonNameText");
    PersonNameText1.InnerText = "담당자이름";
    PrimaryDefinedContact.AppendChild(PersonNameText1);

    XmlNode TelephoneCommunication1 = xdoc.CreateElement("TelephoneCommunication");
    TelephoneCommunication1.InnerText = "전화번호";
    PrimaryDefinedContact.AppendChild(TelephoneCommunication1);

    XmlNode URICommunication1 = xdoc.CreateElement("URICommunication");
    URICommunication1.InnerText = "전자우편";
    PrimaryDefinedContact.AppendChild(URICommunication1);
    InvoiceeParty.AppendChild(PrimaryDefinedContact);


    XmlNode SecondaryDefinedContact = xdoc.CreateElement("SecondaryDefinedContact");
    XmlNode DepartmentNameText2 = xdoc.CreateElement("DepartmentNameText");
    DepartmentNameText2.InnerText = "담당부서";
    SecondaryDefinedContact.AppendChild(DepartmentNameText2);

    XmlNode PersonNameText2 = xdoc.CreateElement("PersonNameText");
    PersonNameText2.InnerText = "담당자이름";
    SecondaryDefinedContact.AppendChild(PersonNameText2);

    XmlNode TelephoneCommunication2 = xdoc.CreateElement("TelephoneCommunication");
    TelephoneCommunication2.InnerText = "전화번호";
    SecondaryDefinedContact.AppendChild(TelephoneCommunication2);

    XmlNode URICommunication2 = xdoc.CreateElement("URICommunication");
    URICommunication2.InnerText = "전자우편";
    SecondaryDefinedContact.AppendChild(URICommunication2);
    InvoiceeParty.AppendChild(SecondaryDefinedContact);

    XmlNode SpecifiedAddress1 = xdoc.CreateElement("SpecifiedAddress");
    XmlNode LineOneText1 = xdoc.CreateElement("LineOneText");
    LineOneText1.InnerText = "서울시 강남구 삼성동";
    SpecifiedAddress1.AppendChild(LineOneText1);
    InvoiceeParty.AppendChild(SpecifiedAddress1);

    //------------------------------------------------------------------
    TaxInvoiceTradeSettlement.AppendChild(InvoiceeParty);

    //===================================================================
    // 3-3 SpecifiedPaymentMeans
    //===================================================================
    XmlNode SpecifiedPaymentMeans = xdoc.CreateElement("SpecifiedPaymentMeans");
    XmlNode TypeCode3 = xdoc.CreateElement("TypeCode");
    TypeCode3.InnerText = "10";
    SpecifiedPaymentMeans.AppendChild(TypeCode3);

    XmlNode PaidAmount = xdoc.CreateElement("PaidAmount");
    PaidAmount.InnerText = "150000";
    SpecifiedPaymentMeans.AppendChild(PaidAmount);

    //------------------------------------------------------------------
    TaxInvoiceTradeSettlement.AppendChild(SpecifiedPaymentMeans);

    //===================================================================
    // 3-4 SpecifiedMonetarySummation
    //===================================================================
    XmlNode SpecifiedMonetarySummation = xdoc.CreateElement("SpecifiedMonetarySummation");
    XmlNode ChargeTotalAmount = xdoc.CreateElement("ChargeTotalAmount");
    ChargeTotalAmount.InnerText = "136364";
    SpecifiedMonetarySummation.AppendChild(ChargeTotalAmount);

    XmlNode TaxTotalAmount = xdoc.CreateElement("TaxTotalAmount");
    TaxTotalAmount.InnerText = "13636";
    SpecifiedMonetarySummation.AppendChild(TaxTotalAmount);

    XmlNode GrandTotalAmount = xdoc.CreateElement("GrandTotalAmount");
    GrandTotalAmount.InnerText = "150000";
    SpecifiedMonetarySummation.AppendChild(GrandTotalAmount);
    //------------------------------------------------------------------
    TaxInvoiceTradeSettlement.AppendChild(SpecifiedMonetarySummation);
    //------------------------------------------------------------------
    root.AppendChild(TaxInvoiceTradeSettlement);
    //===================================================================

    //===================================================================
    // 4. TaxInvoiceTradeLineItem
    //===================================================================

    XmlNode TaxInvoiceTradeLineItem = xdoc.CreateElement("TaxInvoiceTradeLineItem");
    XmlNode equenceNumeric = xdoc.CreateElement("equenceNumeric");
    equenceNumeric.InnerText = "01";
    TaxInvoiceTradeLineItem.AppendChild(equenceNumeric);

    XmlNode InvoiceAmount = xdoc.CreateElement("InvoiceAmount");
    InvoiceAmount.InnerText = "136364";
    TaxInvoiceTradeLineItem.AppendChild(InvoiceAmount);

    XmlNode ChargeableUnitQuantity = xdoc.CreateElement("ChargeableUnitQuantity");
    ChargeableUnitQuantity.InnerText = "1";
    TaxInvoiceTradeLineItem.AppendChild(ChargeableUnitQuantity);

    XmlNode NameText = xdoc.CreateElement("NameText");
    NameText.InnerText = "물품명";
    TaxInvoiceTradeLineItem.AppendChild(NameText);

    XmlNode PurchaseExpiryDateTime = xdoc.CreateElement("PurchaseExpiryDateTime");
    PurchaseExpiryDateTime.InnerText = "20090928";
    TaxInvoiceTradeLineItem.AppendChild(PurchaseExpiryDateTime);

    XmlNode TotalTax = xdoc.CreateElement("TotalTax");
    XmlNode CalculatedAmount = xdoc.CreateElement("CalculatedAmount");
    CalculatedAmount.InnerText = "13636";
    TotalTax.AppendChild(CalculatedAmount);
    TaxInvoiceTradeLineItem.AppendChild(TotalTax);

    XmlNode UnitPrice = xdoc.CreateElement("UnitPrice");
    XmlNode UnitAmount = xdoc.CreateElement("UnitAmount");
    UnitAmount.InnerText = "150000";
    UnitPrice.AppendChild(UnitAmount);
    TaxInvoiceTradeLineItem.AppendChild(UnitPrice);

    //------------------------------------------------------------------
    root.AppendChild(TaxInvoiceTradeLineItem);


    // XML 파일 저장
    xdoc.Save(@"C:\Users\taxInvoice.xml");

}

원래는 전자세금계산서 테이블에서 데이터를 뽑아 xml형식으로 만들거나 아니면 텍스트박스에서 데이터를 받아 xml형식으로 만들어야하는데 그 전에 그냥 1차적으로 하드코딩해서 만들어보았습니다.

 

2. 테이블 정보 가져오기 및 XML생성

관리정보 테이블에 있는 데이터를 가져오고 싶다면?

string szDBFileName = "taxinvoice.db";
string szExecutablePath = Path.GetDirectoryName(Application.ExecutablePath);
// 애플리케이션이 있는 위치 경로
string szDBFile = String.Format(@"{0}\{1}\{2}", szExecutablePath, "Database", szDBFileName);
// DB접속정보 
string constring = string.Format("Data Source={0};Version=3;", szDBFile);

SQLiteConnection dbConnection = new SQLiteConnection();
dbConnection.ConnectionString = constring;
dbConnection.Open();

string sqlCom = "SELECT * FROM EXCHANGEDDOCUMENT";
SQLiteCommand scdCommand = new SQLiteCommand(sqlCom, dbConnection);
SQLiteDataAdapter sdAdapter = new SQLiteDataAdapter();
sdAdapter.SelectCommand = scdCommand;

DataTable dtOut = new DataTable();
sdAdapter.Fill(dtOut);

dbConnection.Close();

이렇게 데이터를 가져온 다음 for문을 통해서 xml 파일을 생성하면 될 것 같습니다

Comments