0

I'm running into some sort of xml error when trying to do a xml request to quickbooks. I created the xml using XDocument in C#, I'm wondering if it is being encoded incorrectly?

Error Msg:

Line: 1 LinePos: 1 Src Text:  Reason: Invalid at the top level of the document.

Here is the start of the xml file

<?xml version="1.0" encoding="utf-8"?>
<?qbxml version="13.0"?>
<QBXML>
  <QBXMLMsgsRq onError="stopOnError">

Here is how I created the xml declaration in C#

XmlDocument POx = new XmlDocument();
XmlDeclaration xmldeclaration = POx.CreateXmlDeclaration("1.0", "UTF-8", null );
XmlElement root = POx.DocumentElement;
POx.InsertBefore(xmldeclaration, root);
Roon
  • 13
  • 4
  • 2
    That `` is surely a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark#UTF-8). It's not part of the `XmlDocument`, rather it is s emitted by the `StreamWriter` when writing to a `Stream`. You don't show the code that does that, however to disable BOMs see [Create Text File Without BOM](https://stackoverflow.com/a/2503049) or [StreamWriter and UTF-8 Byte Order Marks](https://stackoverflow.com/a/11655096). – dbc Oct 05 '17 at 22:53
  • 1
    Thanks dbc, disabling the BOM's fixed my problem. I would upvote your comment if I could. – Roon Oct 06 '17 at 16:37
  • "trying to do a xml request to quickbooks" that's the part that should be stripping off the [BOM](http://unicode.org/faq/utf_bom.html#bom5); it's not part of the document. – Tom Blodget Oct 06 '17 at 21:19

1 Answers1

-2

I usually start a document like code below. It solves a lot of issue with namespaces and is very simple :

            string header = "<?xml version=\"1.0\" encoding=\"utf-8\"?><?qbxml version=\"13.0\"?><QBXML></QBXML>";

            XDocument doc = XDocument.Parse(header);
            XElement qbxml = doc.Root;
jdweng
  • 28,546
  • 2
  • 13
  • 18