【问题标题】:Select a value from XML document, errors with <SalesDataOrderInterface xmlns=''> was not expected从 XML 文档中选择一个值,<SalesDataOrderInterface xmlns=''> 的错误不是预期的
【发布时间】:2021-12-23 01:41:11
【问题描述】:

您好,我正在编辑 Linnworks 宏,过程如下: 获取每次同步的订单 ID 列表; 为每个订单发送 API 调用以检索其 XML 数据; 找到节点“传真”并从中检索日期值; 在 API 中发送日期以更新每个订单的当前订单发货日期。

每个订单都有一个很大的 XML 文件,我无法编辑,只能读取

<SalesDataOrderInterface
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    **XML removed**
    <ExtensionAttributes>
        <ShippingAssignments>
            <SalesDataShippingAssignmentInterface>
                <Shipping>
                    <Address>
                        <AddressType>blank</AddressType>
                        <City>blank</City>
                        <CountryId>blank</CountryId>
                        <CustomerAddressId>0</CustomerAddressId>
                        <CustomerId>0</CustomerId>
                        <Email>blank</Email>
                        <EntityId>0</EntityId>
                        <Fax>27/10/2021</Fax>        **This is the data needed**
                        <Firstname>blank</Firstname>
                        <Lastname>blank</Lastname>
                        <ParentId>0</ParentId>
                        <Postcode>blank</Postcode>
                        <Region>blank</Region>
                        <RegionCode>blank</RegionCode>
                        <RegionId>0</RegionId>
                        <Street>
                            <string>blank</string>
                        </Street>
                        <Telephone>0</Telephone>
                        <VatIsValid>0</VatIsValid>
                        <VatRequestSuccess>0</VatRequestSuccess>
                    </Address>
                    <Method>matrixrates_express_</Method>
                    <Total>
                        <BaseShippingAmount>6</BaseShippingAmount>
                        <BaseShippingCanceled>0</BaseShippingCanceled>
                        <BaseShippingDiscountAmount>0</BaseShippingDiscountAmount>
                        <BaseShippingDiscountTaxCompensationAmnt>0</BaseShippingDiscountTaxCompensationAmnt>
                        <BaseShippingInclTax>6</BaseShippingInclTax>
                        <BaseShippingInvoiced>6</BaseShippingInvoiced>
                        <BaseShippingRefunded>0</BaseShippingRefunded>
                        <BaseShippingTaxAmount>0</BaseShippingTaxAmount>
                        <BaseShippingTaxRefunded>0</BaseShippingTaxRefunded>
                        <ShippingAmount>6</ShippingAmount>
                        <ShippingCanceled>0</ShippingCanceled>
                        <ShippingDiscountAmount>0</ShippingDiscountAmount>
                        <ShippingDiscountTaxCompensationAmount>0</ShippingDiscountTaxCompensationAmount>
                        <ShippingInclTax>6</ShippingInclTax>
                        <ShippingInvoiced>6</ShippingInvoiced>
                        <ShippingRefunded>0</ShippingRefunded>
                        <ShippingTaxAmount>0</ShippingTaxAmount>
                        <ShippingTaxRefunded>0</ShippingTaxRefunded>
                    </Total>
                </Shipping>
                **XML removed**
            </SalesDataShippingAssignmentInterface>
        </ShippingAssignments>
        **XML removed**
    </ExtensionAttributes>
</SalesDataOrderInterface>

这是使用的C#代码,我尝试了很多不同的方法,我对C#的理解很基础。

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using LinnworksAPI;
using System.Xml;

namespace LinnworksMacro
{
    public class LinnworksMacro : LinnworksMacroHelpers.LinnworksMacroBase
    {
        public void Execute(Guid[] OrderIds) //Gets all ordersIds into table
        {
            try
            {
                Logger.WriteInfo("Macro started");

                List<OrderDetails> orderDetails = new List<OrderDetails>();

                for(int i = 0; i < OrderIds.Count(); i += 200) //Increment blocks 200
                {
                    orderDetails.AddRange(Api.Orders.GetOrdersById(OrderIds.Skip(i).Take(200).ToList())); //Adds order details to list
                }

                foreach(var orderDetail in orderDetails) //Loop through every order recieved
                {
                    Logger.WriteInfo($"Order: {orderDetail.OrderId}");
                    List<OrderXML> xmlList = Api.Orders.GetOrderXml(orderDetail.OrderId); //Gets XML for an order

                    if (xmlList.Count() == 0) //Check if XML exists
                    {
                        Logger.WriteError($"No XML found for order {orderDetail.OrderId}"); //If not then error
                        continue;
                    }
                        string xml = xmlList.First().XML;
                        XmlDocument xmlDoc = new XmlDocument();

                        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
                        nsmgr.AddNamespace("ns1", "http://www.w3.org/2001/XMLSchema");
                        nsmgr.AddNamespace("ns2", "http://www.w3.org/2001/XMLSchema-instance");
                        xmlDoc.LoadXml(xml);
                        string date = xmlDoc.SelectSingleNode("//ns1:Fax",nsmgr).InnerText;

                        DateTime newDate = new DateTime();
                        if (DateTime.TryParseExact(date, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out newDate))
                        //Attempt to parse fax shipping address into a date + enter into newDate
                        {
                            Logger.WriteInfo($"Old Date: {orderDetail.GeneralInfo.DespatchByDate}. New Date: {date}");
                            orderDetail.GeneralInfo.DespatchByDate = newDate.AddHours(16); //Add 16 hours to make 16:00 pm
                            Api.Orders.SetOrderGeneralInfo(orderDetail.OrderId, orderDetail.GeneralInfo, false); //Add new dispatch date to order
                        }
                        else
                        {
                            Logger.WriteError($"Could not parse date {date}, for order {orderDetail.NumOrderId}"); //Error
                        }

                }
            }
            catch (Exception ex)
            {
                Logger.WriteError($"Error: {ex.Message} at: {ex.StackTrace}"); //Catch error
                throw ex;
            }
            finally
            {
                Logger.WriteInfo("Macro Finished");
            }
        }
    }
}

出现的错误是-

Macro execute failed with error: '<SalesDataOrderInterface xmlns=''> was not expected.'

我不确定这是指哪一行。

任何帮助将不胜感激。

最好的问候, 乔什

【问题讨论】:

  • 有什么想法吗?我尝试了几种不同的读取 XML 的方法,它们都出现了相同的错误。

标签: c# xml


【解决方案1】:

问题已解决,取出命名空间管理器并更正一个错字

string xml = xmlList.First().XML;

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml);

XmlNode xmlNode = xmlDoc.SelectSingleNode("/SalesDataOrderInterface/ExtensionAttributes/ShippingAssignments/SalesDataShippingAssignmentInterface/Shipping/Address/Fax");
string fdate = xmlNode.InnerText;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多