提问人:lucianobonde 提问时间:4/19/2023 最后编辑:marc_slucianobonde 更新时间:4/20/2023 访问量:152
如何将 xml 反序列化为对象 Net 6?
How deserialize xml to object Net 6?
问:
我在输入中有xml字符串:
<ActivationRequest xmlns="http://schemas.datacontract.org/2004/07/DataLayer.Models" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<Key>key</Key>
<Edition i:nil="true"/>
<HardwareId>542D</HardwareId>
<IntelliLockFlags>BIOS CPU</IntelliLockFlags>
<MachineName>nikolas</MachineName>
<Product>studio</Product>
<UseMacHID>false</UseMacHID>
<Version xmlns:a="http://schemas.datacontract.org/2004/07/System">
<a:_Build>88</a:_Build>
<a:_Major>6</a:_Major>
<a:_Minor>4</a:_Minor>
<a:_Revision>0</a:_Revision>
</Version>
</ActivationRequest>
还有我的解串器:
public static Parameters DeserializeFromXml<T>(this string xml)
{
using (var stream = new MemoryStream())
{
byte[] data = Encoding.UTF8.GetBytes(xml);
stream.Write(data, 0, data.Length);
stream.Position = 0;
var deserializer = new DataContractSerializer(typeof(Parameters));
return deserializer.ReadObject(stream) as Parameters; // error
}
}
型号等级:
public class Parameters
{
public string Key { get; set; }
public string HardwareId { get; set; }
public string Product { get; set; }
public Version Version { get; set; }
public bool UseMacHID { get; set; }
public LockFlags IntelliLockFlags { get; set; }
public string MachineName { get; set; }
}
[Flags]
public enum LockFlags
{
Empty = 0,
BIOS = 1,
CPU = 2
}
这在 .Net Core 2.2 上有效。但是在 .Net 6 上更改版本后,我开始出现错误:
第 1 行位置 144 中的错误。从命名空间“http://schemas.datacontract.org/2004/07/DataLayer.NetStandard.BusinessLayer.Activation”中期待元素“ActivationParameters”。遇到名称为“ActivationRequest”,命名空间为“http://schemas.datacontract.org/2004/07/DataLayer.Models”的“元素”。
任何想法
答:
1赞
Serge
4/19/2023
#1
这对我有用
XmlSerializer serializer = new XmlSerializer(typeof(ActivationRequest));
using (TextReader reader = new StringReader(xml))
{
ActivationRequest result = (ActivationRequest)serializer.Deserialize(reader);
}
类
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.datacontract.org/2004/07/DataLayer.Models")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.datacontract.org/2004/07/DataLayer.Models", IsNullable = false)]
public partial class ActivationRequest
{
public string KeyField { get; set; }
public string Key
{
get
{
return this.KeyField;
}
set
{
this.KeyField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(IsNullable = true)]
public object Edition { get; set; }
public string HardwareId { get; set; }
public string IntelliLockFlags { get; set; }
public string MachineName { get; set; }
public string Product { get; set; }
public bool UseMacHID { get; set; }
public ActivationRequestVersion Version { get; set; }
}
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.datacontract.org/2004/07/DataLayer.Models")]
public partial class ActivationRequestVersion
{
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://schemas.datacontract.org/2004/07/System")]
public byte _Build { get; set; }
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://schemas.datacontract.org/2004/07/System")]
public byte _Major { get; set; }
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://schemas.datacontract.org/2004/07/System")]
public byte _Minor { get; set; }
[System.Xml.Serialization.XmlElementAttribute(Namespace = "http://schemas.datacontract.org/2004/07/System")]
public byte _Revision { get; set; }
}
评论
ActivationRequest
Paramaters