C#.NET 并解析来自服务器的 XML 响应

问题描述 投票:0回答:3

我拥有多年使用 JS、Python 和 JSON 的经验,但在过去一周才开始使用 C#.NET 和 XML。

我必须为 Quickbooks Desktop 编写一种解决方案,以将多个发票行项目合并为一个行项目。我从 Intuit 得到的例子已经有 10 多年的历史了。从 SO 主题中,我并不完全清楚什么是适合我的用例的现代方法:答案要么是 6 年前的答案,要么与从文件中读取有关——我正在尝试解析响应。

这是服务器响应的样子(我暂时将其写入文件),然后是一些问题:

<QBXML>
  <QBXMLMsgsRs>
    <InvoiceQueryRs requestID="0" statusCode="0" statusSeverity="Info" statusMessage="Status OK">

      <!-- RECORD 1 -->
      <InvoiceRet>
        <TxnID>3B58B-1540309911</TxnID>
        <EditSequence>1540587328</EditSequence>
        <TxnNumber>27058</TxnNumber>
        <CustomerRef>
          <ListID>800006FF-1540307596</ListID>
          <FullName>Test Co.:Test 1</FullName>
        </CustomerRef>
        <RefNumber>L-9</RefNumber>
        <Subtotal>2212.00</Subtotal>
        <BalanceRemaining>2212.00</BalanceRemaining>
        <IsPaid>false</IsPaid>

        <!-- RECORD 1: INVOICE LINE ITEM 1 -->
        <InvoiceLineRet>
          <TxnLineID>3B58D-1540309911</TxnLineID>
          <ItemRef>
            <ListID>80000002-1443563699</ListID>
            <FullName>Consulting Fees</FullName>
          </ItemRef>
          <Desc>PROJECT: Test 1; TASK: Wrap-up</Desc>
          <Quantity>6</Quantity>
          <Rate>220</Rate>
          <Amount>1320.00</Amount>
        </InvoiceLineRet>

        <!-- RECORD 1: INVOICE LINE ITEM 2 -->
        <InvoiceLineRet>
          <TxnLineID>3B58E-1540309911</TxnLineID>
          <ItemRef>
            <ListID>80000002-1443563699</ListID>
            <FullName>Consulting Fees</FullName>
          </ItemRef>
          <Desc>PROJECT: Test 1; TASK: Kickoff</Desc>
          <Quantity>4</Quantity>
          <Rate>220</Rate>
          <Amount>880.00</Amount>
        </InvoiceLineRet>

        <!-- RECORD 1: INVOICE LINE ITEM 3 -->
        <InvoiceLineRet>
          <TxnLineID>3B58F-1540309911</TxnLineID>
          <ItemRef>
            <ListID>80000025-1538518495</ListID>
            <FullName>Travel:Travel Meals</FullName>
          </ItemRef>
          <Desc>DATE: Oct 23, 2018; PROJECT: Test 1</Desc>
          <Rate>12</Rate>
          <Amount>12.00</Amount>
        </InvoiceLineRet>
      </InvoiceRet>

      <!-- RECORD 2 -->
      <InvoiceRet>
        <TxnID>3B595-1540830324</TxnID>
        <EditSequence>1540830324</EditSequence>
        <TxnNumber>27060</TxnNumber>
        <CustomerRef>
          <ListID>80000700-1540307618</ListID>
          <FullName>Test Co.:Test 2</FullName>
        </CustomerRef>
        <RefNumber>L-11</RefNumber>
        <Subtotal>2760.00</Subtotal>
        <BalanceRemaining>2760.00</BalanceRemaining>

        <!-- RECORD 2: INVOICE LINE ITEM 1 -->
        <InvoiceLineRet>
          <TxnLineID>3B597-1540830324</TxnLineID>
          <ItemRef>
            <ListID>80000002-1443563699</ListID>
            <FullName>Consulting Fees</FullName>
          </ItemRef>
          <Desc>PROJECT: Test 2; TASK: Prebill Task</Desc>
          <Rate>1000</Rate>
          <Amount>1000.00</Amount>
        </InvoiceLineRet>

        <!-- RECORD 2: INVOICE LINE ITEM 2 -->
        <InvoiceLineRet>
          <TxnLineID>3B598-1540830324</TxnLineID>
          <ItemRef>
            <ListID>80000002-1443563699</ListID>
            <FullName>Consulting Fees</FullName>
          </ItemRef>
          <Desc>PROJECT: Test 2; TASK: Present Findings</Desc>
          <Quantity>3</Quantity>
          <Rate>0.00</Rate>
          <Amount>0.00</Amount>
        </InvoiceLineRet>

        <!-- RECORD 2: INVOICE LINE ITEM 3 -->
        <InvoiceLineRet>
          <TxnLineID>3B599-1540830324</TxnLineID>
          <ItemRef>
            <ListID>80000002-1443563699</ListID>
            <FullName>Consulting Fees</FullName>
          </ItemRef>
          <Desc>PROJECT: Test 2; TASK: Research</Desc>
          <Quantity>2</Quantity>
          <Rate>220</Rate>
          <Amount>440.00</Amount>
        </InvoiceLineRet>

        <!-- RECORD 2: INVOICE LINE ITEM 4 -->
        <InvoiceLineRet>
          <TxnLineID>3B59A-1540830324</TxnLineID>
          <ItemRef>
            <ListID>80000002-1443563699</ListID>
            <FullName>Consulting Fees</FullName>
          </ItemRef>
          <Desc>PROJECT: Test 2; TASK: Project Launch</Desc>
          <Quantity>6</Quantity>
          <Rate>220</Rate>
          <Amount>1320.00</Amount>
        </InvoiceLineRet>
      </InvoiceRet>

    </InvoiceQueryRs>
  </QBXMLMsgsRs>
</QBXML>
  1. 一般来说,解析此内容以便可以引用每个元素的现代方法是什么?

类似

xml.InvoiceRet[0].TxnId
的东西就是
3B58B-1540309911

  1. 如果父节点
    <InvoiceRet>
    不伦不类,每条记录如何被引用?您是否必须迭代每个
    <InvoiceRet>

我从 Intuit 文档中尝试过的内容:

// this is where the query is sent to the QB and response is returned
var responseSet = sessionManager.DoRequests(requestSet);

// take the response and get the first element, I guess?
var response = responseSet.ResponseList.GetAt(0);
var customerRetList = response.Detail as ICustomerRetList;

// print the number of records
// it should be 2, but comes back with nothing
Console.WriteLine(customerRetList.Count);
c# .net xml quickbooks qbxml
3个回答
1
投票

我将结果放入数据表中,因此结果是平坦的。我从文件中得到了结果。 Load 方法可以使用 URL 或文件名。如果您有字符串,请将 Load() 替换为 Parse()。

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

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("TxnID", typeof(string));
            dt.Columns.Add("EditSequence", typeof(string));
            dt.Columns.Add("TxnNumber", typeof(string));
            dt.Columns.Add("CustomerRef_ListID", typeof(string));
            dt.Columns.Add("CustomerRef_FullName", typeof(string));
            dt.Columns.Add("RefNumber", typeof(string));
            dt.Columns.Add("Subtotal", typeof(decimal));
            dt.Columns.Add("BalanceRemaining", typeof(decimal));
            dt.Columns.Add("IsPaid", typeof(Boolean));
            dt.Columns.Add("ItemRef_ListID", typeof(string));
            dt.Columns.Add("ItemRef_FullName", typeof(string));
            dt.Columns.Add("Desc", typeof(string));
            dt.Columns.Add("Quantity", typeof(int));
            dt.Columns.Add("Rate", typeof(decimal));
            dt.Columns.Add("Amount", typeof(decimal));


            XDocument doc = XDocument.Load(FILENAME);

            foreach (XElement invoiceRet in doc.Descendants("InvoiceRet"))
            {
                string txnId = (string)invoiceRet.Element("TxnID");
                string editSequence = (string)invoiceRet.Element("EditSequence");
                string txnNumber = (string)invoiceRet.Element("TxnNumber");
                XElement customerRef = invoiceRet.Element("CustomerRef");
                string custListId = (string)customerRef.Element("ListID");
                string custFullName = (string)customerRef.Element("FullName");
                string refNumber = (string)invoiceRet.Element("RefNumber");
                decimal subtotal = (decimal)invoiceRet.Element("Subtotal");
                decimal balance = (decimal)invoiceRet.Element("BalanceRemaining");
                Boolean? isPaid = (Boolean?)invoiceRet.Element("IsPaid");

                foreach (XElement invoiceLine in invoiceRet.Elements("InvoiceLineRet"))
                {
                    string lineListId = (string)invoiceLine.Descendants("ListID").FirstOrDefault();
                    string lineFullName = (string)invoiceLine.Descendants("FullName").FirstOrDefault();
                    string desc = (string)invoiceLine.Element("Desc");
                    int? quantity = (int?)invoiceLine.Element("Quantity");
                    decimal rate = (decimal)invoiceLine.Element("Rate");
                    decimal amount = (decimal)invoiceLine.Element("Amount");

                    dt.Rows.Add(new object[] {
                        txnId, editSequence, txnNumber, custListId, custFullName,
                        refNumber, subtotal, balance, isPaid, 
                        lineListId, lineFullName, desc, quantity, rate, amount 
                    });


                }
            }
        }
    }
}

0
投票

对于我的用例来说,基本查询(LINQ to XML)将是最好的选择。决定使用以下方式解析对字符串的响应:

// Do the request and get the response message set object
var responseSet = sessionManager.DoRequests(requestSet);

// Convert response to string
var responseXml = responseSet.ToXMLString();

// Then the LINQ to XML for the string that I'm still working through
var xmlDoc = XDocument.Parse(responseXml);

// Set the xmlDoc root
var xmlDocRoot = xmlDoc.Root.Element("QBXMLMsgsRs")
                            .Element("InvoiceQueryRs")
                            .Elements("InvoiceRet");


// Iterate through the elements to get values and do some logic
foreach (var invoiceElement in xmlDocRoot)
{
    var editSequence = (string) invoiceElement.Element("EditSequence");
}

0
投票

按照这些步骤将生成 XML 之外的 C# 对象(前提是您有源 XSD 文件)

第 1 步 -- 获取相关的 XSD 文件

从 XML SDK 提供商提供的 XSD 文件创建类。在您的情况下,它是 QuickBooks SDK。根据 QuickBooks 软件的目标版本,从路径中获取相关的 XSD 文件:

C:\Program Files (x86)\Intuit\IDN\Common ools alidator

第 2 步 -- 从 XSD 定义文件创建类。

针对您的父 xsd 文件运行此 xsd 命令,例如

xsd QBCqbxmlops160.cs.xsd /classes

第 3 步 -- 将课程包含到您的项目中

将生成的 C# 类文件包含到您的项目中。该文件包含 QBXML 父节点中所有 XML 规范的 C# 类、ENUM、数据类型转换的完美翻译。

第 4 步 -- 将 XML 转换为 C# 对象

//https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlserializer.deserialize?view=net-7.0

string xmlResponseSgtring = "<QBXML><QBXMLMsgsRs>...";
XmlReader xmlReader = XmlReader.Create(new StringReader(xmlResponseSgtring));
QBXML qbXmlResponseObject;
qbXmlResponseObject = (QBXML)serializer.Deserialize(xmlReader);
© www.soinside.com 2019 - 2024. All rights reserved.