从 XML 文档中选择一个值,<销售数据订单接口 xmlns=> 错误是预期的



嗨,我正在编辑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.'

我不确定那是指哪一行。

任何帮助都将不胜感激。

谨致问候,Josh

问题得到解决,取出命名空间管理器并更正了一个拼写错误

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;

最新更新