如何通过QBFC使用XML文本文件添加发票?预生成QBXML请求发送咨询
Got it, I’ve faced this exact roadblock with QBFC before—its DoRequests() method is rigid about requiring an IMsgSetRequest object, but there’s a straightforward way to repurpose your pre-generated QBXML to fit that requirement. Here’s how to make it work:
Step 1: Get Your Pre-Generated QBXML as a String
First, you’ll need to extract the raw QBXML content from your .gdoc file. Since .gdoc is a Google Docs proprietary format, export it first as a plain text or XML file (you can do this manually via Google Docs’ "File > Download" menu, or automate it with the Google Drive API if needed). Once you have the raw XML, read it into a string in your C# code:
// Example: Read QBXML from a plain text file exported from .gdoc string preGeneratedQBXML = File.ReadAllText(@"C:\path\to\your\exported-qbxml.txt");
Step 2: Load the QBXML String into an IMsgSetRequest
QBFC’s MsgSetRequest class has a hidden gem: the LoadXML() method, which lets you convert a raw QBXML string directly into the IMsgSetRequest object that DoRequests() expects. Here’s the full code flow:
using QBFC13Lib; // Adjust the QBFC version namespace to match your QB installation // Initialize QBFC session var sessionManager = new QBSessionManager(); try { // Open connection to QuickBooks Desktop sessionManager.OpenConnection("", "Your Application Name"); sessionManager.BeginSession("", ENOpenMode.omDontCare); // Create a base MsgSetRequest (match region and version to your QB instance) IMsgSetRequest requestMsgSet = sessionManager.CreateMsgSetRequest("US", 13.0); // Load your pre-generated QBXML into the request object requestMsgSet.LoadXML(preGeneratedQBXML); // Send the request just like you would with a built-from-scratch request IMsgSetResponse responseMsgSet = sessionManager.DoRequests(requestMsgSet); // Handle the response (parse errors, confirm success, etc.) if (responseMsgSet.ResponseList.Count > 0) { var response = responseMsgSet.ResponseList.GetAt(0); if (response.StatusCode != 0) { Console.WriteLine($"Error: {response.StatusMessage}"); } else { Console.WriteLine("Invoice added successfully!"); } } } catch (Exception ex) { Console.WriteLine($"QBFC Error: {ex.Message}"); } finally { // Clean up session if (sessionManager.SessionOpen) sessionManager.EndSession(); if (sessionManager.ConnectionOpen) sessionManager.CloseConnection(); }
Key Notes to Avoid Issues:
- QB Version Matching: The version number (e.g.,
13.0) must match your QuickBooks Desktop version. For example:- QB 2022 = 13.0
- QB 2023 = 14.0
- QB 2024 = 15.0
Mismatched versions will causeLoadXML()orDoRequests()to fail.
- QBXML Validation: Ensure your pre-generated QBXML adheres to the correct QBXML schema for your QB version. Use QuickBooks’ official QBXML Validator Tool to check for syntax or schema errors before loading it.
- Root Node Structure: Your QBXML must start with the standard root nodes:
If your .gdoc output misses these wrapping nodes, add them before loading the string.<?xml version="1.0" encoding="utf-8"?> <QBXML> <QBXMLMsgsRq onError="stopOnError"> <!-- Your invoice add request here --> </QBXMLMsgsRq> </QBXML>
Alternative: Direct XML Transmission (If QBFC Isn’t a Hard Requirement)
If you’d rather skip QBFC entirely, you can use QuickBooks Desktop’s XML Gateway to send raw QBXML via HTTP, but this requires setting up the QuickBooks Web Connector or using direct socket communication. That said, the LoadXML() method above is the simplest path if you’re already using QBFC.
内容的提问来源于stack exchange,提问作者Derek Glissman

