简体   繁体   English

从 XML 文档中选择一个值,错误为<SalesDataOrderInterface xmlns=''>没想到

[英]Select a value from XML document, errors with <SalesDataOrderInterface xmlns=''> was not expected

Hi I'm editing a Linnworks macro, this is the process: get a list of Order Ids each sync;嗨,我正在编辑 Linnworks 宏,过程如下:获取每次同步的订单 ID 列表; send an API call for each order to retrieve its XML data;为每个订单发送 API 调用以检索其 XML 数据; locate the node "Fax" and retrieve the date value from it;找到节点“传真”并从中检索日期值; send the date in an API to update the current order dispatch date for each order.在 API 中发送日期以更新每个订单的当前订单发货日期。

Each order has a large XML file which I cannot edit, only read每个订单都有一个很大的 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>

This is the C# code used, I've tried a number of different ways of doing it, my understanding of C# is quite basic.这是使用的 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");
            }
        }
    }
}

The error that comes up is -出现的错误是——

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

I'm not sure which line that is referring to.我不确定指的是哪一行。

Any help will be appreciated.任何帮助将不胜感激。

Best regards, Josh最好的问候,乔希

Issue resolved, took out the namespace manager and corrected a typo问题已解决,取出命名空间管理器并更正了一个错字

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM