We help IT Professionals succeed at work.
Get Started

Merge XML after existing XML to create QBXML FILE

114 Views
Last Modified: 2019-12-19
I have my code in C# to create the outershell of the file I need(QBXML).
I then have it call my function to query SQL and get the info that need to go inside of it under the <QBXMLMsgRq> element

The first part creates this -
<?xml version="1.0"?>
<?qbxml version="13.0"?>
<QBXML>
  <QBXMLMsgsRq onError="stopOnError" />
</QBXML>

Open in new window


Then it called my "BuildCustomerAddRq"

Which is this
 void BuildCustomerAddRq(XmlDocument doc, XmlElement parent)
    {

        try
        {
            string ConString = "Data Source=192.168.0.40;Initial Catalog=OCITIME;User ID=ocisql;Password=Emagdne1212";
            string CmdString = "select employee_name as Name ,'OCI Associates' as [CompanyName],'Mr' as Salutation , LEFT(employee_name, CHARINDEX(' ', employee_name + ' ') - 1) as FirstName,  REVERSE(LEFT(REVERSE(employee_name),    CHARINDEX(' ', REVERSE(employee_name)) - 1)) AS LastName   from EMPLOYEELIST for xml raw('CustomerAdd') , ROOT('CustomerAddRq'), ELEMENTS";

            SqlConnection con;
            SqlCommand cmd;
            XmlReader reader;
            XmlDocument xmlDoc;

            using (con = new SqlConnection(ConString))
            {
                cmd = new SqlCommand(CmdString, con);
                con.Open();
                reader = cmd.ExecuteXmlReader();
          

                while (reader.Read())
                {
             
                    doc.Load(reader);
                   
                }
                doc.AppendChild(doc.CreateXmlDeclaration("1.0", null, null));
                doc.AppendChild(doc.CreateProcessingInstruction("qbxml", "version=\"13.0\""));
                doc.Save("C:\\Employees.xml");
            }
        }
        catch (Exception)
        {
            throw;
        }


    }

Open in new window


Here is the build request that runs first -

//This is the shit to create requests and submit to QUICKBOOKS
    public ArrayList buildRequest()
    {
        XmlDocument requestXmlDoc = new XmlDocument();
        //Add the prolog processing instructions
        requestXmlDoc.AppendChild(requestXmlDoc.CreateXmlDeclaration("1.0", null, null));
        requestXmlDoc.AppendChild(requestXmlDoc.CreateProcessingInstruction("qbxml", "version=\"13.0\""));

        //Create the outer request envelope tag
        XmlElement outer = requestXmlDoc.CreateElement("QBXML");
        requestXmlDoc.AppendChild(outer);

        //Create the inner request envelope & any needed attributes
        XmlElement inner = requestXmlDoc.CreateElement("QBXMLMsgsRq");
        outer.AppendChild(inner);
        inner.SetAttribute("onError", "stopOnError");
        BuildCustomerAddRq(requestXmlDoc, inner);
       

        string strRequestXML = requestXmlDoc.OuterXml;
        req.Add(strRequestXML);
        requestXmlDoc.Save("C:\\testing123.xml");




        return req;
    }

Open in new window


I need it to spit something out like this -

<?xml version="1.0"?>
<?qbxml version="13.0"?>
<QBXML>
  <QBXMLMsgsRq onError="stopOnError">
    <CustomerAddRq requestID="1">
      <CustomerAdd>
        <Name>Fake Name</Name>
        <CompanyName>Fake Company</CompanyName>
        <Salutation>Mr.</Salutation>
        <FirstName>Fake</FirstName>
        <MiddleName>P</MiddleName>
        <LastName>Name</LastName>
        <JobTitle>Systems Engineer</JobTitle>
        <BillAddress>
          <Addr1>1234 Fake Address</Addr1>
        </BillAddress>
        <ShipToAddress>
          <Name>Fake Name</Name>
        </ShipToAddress>
        <Phone>123-126-123</Phone>
        <AltPhone>123-123-123</AltPhone>
        <Email>gashlin@gmail.com</Email>
      </CustomerAdd>
    </CustomerAddRq>
  </QBXMLMsgsRq>
</QBXML>

Open in new window



I know it has to be something simple but am drawing a blank.

Currently it is only giving me this

<CustomerAddRq>
  <CustomerAdd>
    <Name>Fake Name</Name>
    <CompanyName>Fake Company</CompanyName>
    <Salutation>Mr</Salutation>
    <FirstName>Fake</FirstName>
    <LastName>Name</LastName>
  </CustomerAdd>
</CustomerAddRq>

Open in new window


Looking to have them combined properly
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE