我正在尝试将XML字符串转换为c#中的类对象,但会出现异常。为此,我尝试使用以下代码将xml字符串转换为类对象。
string xmlString = @"<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<WdashboardRoot xmlns='urn:microsoft-dynamics-nav/xmlports/x14027120'>
<Whsedashboard1>
<DocumentType> Order </DocumentType>
<OrderNumber> S - ORD101006 </OrderNumber>
<CustomerNo> 10000 </CustomerNo>
<CustomerName> Adatum Corporation </CustomerName>
<ShippingAgentCode> UPS </ShippingAgentCode>
<ShippingAgentService> GROUND </ShippingAgentService>
<ReqShipmentDate> 04 / 02 / 21 </ReqShipmentDate>
<PromiseShipmentDate> 04 / 02 / 21 </PromiseShipmentDate>
<Status> Pending Orders </Status>
</Whsedashboard1>
</WdashboardRoot> ";
Serializer ser = new Serializer();
WdashboardRoot Wdashboard = ser.Deserialize<WdashboardRoot>(xmlString);
var xmlOutputData = ser.Serialize<WdashboardRoot>(Wdashboard);
[Serializable]
public class WdashboardRoot
{
public Whsedashboard1[] Whsedashboard1
}
[Serializable]
public class Whsedashboard1
{
public string DocumentType { get; set; }
public string OrderNumber { get; set; }
public string CustomerNo { get; set; }
public string CustomerName { get; set; }
public string ShippingAgentCode { get; set; }
public string ShippingAgentService { get; set; }
public string ReqShipmentDate { get; set; }
public string PromiseShipmentDate { get; set; }
public string Status { get; set; }
}
public class Serializer
{
public T Deserialize<T>(string input) where T : class
{
System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(T));
using (StringReader sr = new StringReader(input))
{
return (T)ser.Deserialize(sr);
}
}
public string Serialize<T>(T ObjectToSerialize)
{
XmlSerializer xmlSerializer = new XmlSerializer(ObjectToSerialize.GetType());
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, ObjectToSerialize);
return textWriter.ToString();
}
}
}请您建议我将这个xml字符串转换为类对象。
真的很感激有人帮忙。
发布于 2021-04-16 08:30:42
我的工作是做些小改变。见下文
[XmlRoot(Namespace = "urn:microsoft-dynamics-nav/xmlports/x14027120")]
public class WdashboardRoot
{
[XmlElement()]
public Whsedashboard1[] Whsedashboard1 { get; set; }
}

https://stackoverflow.com/questions/67120175
复制相似问题