gpt4 book ai didi

c# - < Material xmlns ='x' > 不是预期的

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:54 25 4
gpt4 key购买 nike

我正在尝试使用以下方法反序列化分层 xml 文件

    public static T Deserialize<T>(this string serializedObj) where T : class
{
XmlSerializer xs = new XmlSerializer(typeof(T));
T result;

using (TextReader reader = new StringReader(serializedObj))
result = xs.Deserialize(reader) as T;

return result;
}

但他总是因为在 xml 文件中引用了架构而我不知道哪里出了问题。我试图反序列化为一个对象,但即使在那里他仍然失败。

尝试反序列化xml的函数如下

    public static void RecieveAndSaveXml(string fullPath, string basePath, string fileName)
{
if (File.Exists(fullPath))
{
StreamReader reader = new StreamReader(fullPath);
string xmlString = reader.ReadToEnd();
Material material = xmlString.Deserialize<Material>();

//Conversion to dataobject goes here

reader.Close();
}
}

我要反序列化到的类

[Serializable]
[XmlRoot("Material")]
public class Material
{
public Material() { }

[XmlAttribute("MatNr")]
public string MatNr { get; set; }
[XmlAttribute("MatDescrEN")]
public string MatDescrEN { get; set; }
[XmlAttribute("MatDescrNL")]
public string MatDescrNL { get; set; }
[XmlAttribute("MatDescrFR")]
public string MatDescrFR { get; set; }
[XmlAttribute("OldMatNr")]
public string OldMatNr { get; set; }
[XmlAttribute("UoM")]
public string UoM { get; set; }
[XmlAttribute("MatGroupCode")]
public string MatGroupCode { get; set; }
[XmlAttribute("Extract")]
public decimal Extract { get; set; }
[XmlArray("AdditionalData"), XmlArrayItem(ElementName = "AddDataRecord", Type = typeof(AddDataRecord))]
public AddDataRecord[] AdditionalData { get; set; }
[XmlArray("PlantData"), XmlArrayItem(ElementName = "PlantDataRecord", Type = typeof(PlantDataRecord))]
public PlantDataRecord[] PlantData { get; set; }
}

[Serializable]
[XmlRoot("AddDataRecord")]
public class AddDataRecord
{
[XmlAttribute("UoMDenom")]
public int UoMDenom { get; set; }
[XmlAttribute("UoMAlt")]
public string UoMAlt { get; set; }
[XmlAttribute("UoMNum")]
public int UoMNum { get; set; }
[XmlAttribute("UoMBase")]
public string UoMBase { get; set; }
}

[Serializable]
[XmlRoot("PlantDataRecord")]
public class PlantDataRecord
{
[XmlAttribute("Plant")]
public string Plant { get; set; }
[XmlAttribute("Recipient")]
public string Recipient { get; set; }
[XmlAttribute("Status")]
public string Status { get; set; }
}

还有xml

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Materials xmlns:ns0="www.foo.be/foo/">
<Material>
<MatNr>000000000050165478</MatNr>
<MatDescrEN>KEG CAP GREEN PR JUP NA 1/2008</MatDescrEN>
<MatDescrNL></MatDescrNL>
<MatDescrFR></MatDescrFR>
<OldMatNr>000000000000216505</OldMatNr>
<UoM>PCE</UoM>
<MatGroupCode>020506</MatGroupCode>
<Extract></Extract>
<AdditionalData>
<AddDataRecord>
<UoMDenom>1</UoMDenom>
<UoMAlt>PCE</UoMAlt>
<UoMNum>1</UoMNum>
<UoMBase>PCE</UoMBase>
</AddDataRecord>
</AdditionalData>
<PlantData>
<PlantDataRecord>
<Plant>BE01</Plant>
<Recipient></Recipient>
<Status>99</Status>
</PlantDataRecord>
</PlantData>
</Material>
</ns0:Materials>

所以我试过的是

  • 将嵌套类放在注释中
  • 反序列化为对象
  • 更改列表类型
  • 使用 XmlArrayItem 将注释从 XmlAttribute 更改为 XmlArray
  • 添加了 XmlRootItems

所以我不知道接下来要尝试什么,所以这就是我在这里发帖的原因。提前致谢

最佳答案

与提供的 XML 文件相比,您的类存在一些问题:

  • 所有属性都是示例 xml 中的元素,但它们在您的 C# 类中具有 XmlAttribute
  • 您有一个不可为 null 的十进制值 (Extract),对应的 xml 元素为空。如果这是一个有效的案例,您需要以某种方式处理它。
  • 您的 xml 表示 Material 的集合,但您试图将其反序列化为 Material 的单个实例。

更新代码:

[Serializable]
[XmlRoot("Material")]
public class Material
{
public Material() { }

[XmlElement("MatNr")]
public string MatNr { get; set; }
[XmlElement("MatDescrEN")]
public string MatDescrEN { get; set; }
[XmlElement("MatDescrNL")]
public string MatDescrNL { get; set; }
[XmlElement("MatDescrFR")]
public string MatDescrFR { get; set; }
[XmlElement("OldMatNr")]
public string OldMatNr { get; set; }
[XmlElement("UoM")]
public string UoM { get; set; }
[XmlElement("MatGroupCode")]
public string MatGroupCode { get; set; }
[XmlElement("Extract")]
public String ExtractString { get; set; }
[XmlIgnore]
public decimal Extract
{
get { return String.IsNullOrWhiteSpace(this.ExtractString) ? 0M : decimal.Parse(this.ExtractString); }
set { this.ExtractString = this.Extract.ToString(CultureInfo.InvariantCulture); }
}
[XmlArray("AdditionalData"), XmlArrayItem(ElementName = "AddDataRecord", Type = typeof(AddDataRecord))]
public AddDataRecord[] AdditionalData { get; set; }
[XmlArray("PlantData"), XmlArrayItem(ElementName = "PlantDataRecord", Type = typeof(PlantDataRecord))]
public PlantDataRecord[] PlantData { get; set; }
}

[Serializable]
[XmlRoot("AddDataRecord")]
public class AddDataRecord
{
[XmlElement("UoMDenom")]
public int UoMDenom { get; set; }
[XmlElement("UoMAlt")]
public string UoMAlt { get; set; }
[XmlElement("UoMNum")]
public int UoMNum { get; set; }
[XmlElement("UoMBase")]
public string UoMBase { get; set; }
}

[Serializable]
[XmlRoot("PlantDataRecord")]
public class PlantDataRecord
{
[XmlElement("Plant")]
public string Plant { get; set; }
[XmlElement("Recipient")]
public string Recipient { get; set; }
[XmlElement("Status")]
public string Status { get; set; }
}

[XmlRoot("Materials", Namespace = "www.foo.be/foo/")]
[Serializable]
public class MaterialCollection
{
[XmlElement("Material", Namespace="")]
public List<Material> Materials { get; set; }
}

然后你需要使用

MaterialCollection materials = xmlString.Deserialize<MaterialCollection>();

关于c# - < Material xmlns ='x' > 不是预期的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23423244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com