将XML反序列化为具有意外类型(2,2)的C#类错误
目标是将XML文件反序列化为c#对象。 我已经使用site创建了对象类 当我尝试读取XML文件时,出现错误: XML文档(2,2)包含错误。 没想到 显然,部分课程有些地方缺失/错误,但我太新了,无法理解。 有人能帮我吗 示例头XML将XML反序列化为具有意外类型(2,2)的C#类错误,c#,xml,serialization,C#,Xml,Serialization,目标是将XML文件反序列化为c#对象。 我已经使用site创建了对象类 当我尝试读取XML文件时,出现错误: XML文档(2,2)包含错误。 没想到 显然,部分课程有些地方缺失/错误,但我太新了,无法理解。 有人能帮我吗 示例头XML 1. 3. SF20-0178-BO 层/ 24/09/2020 C级 使用系统; 使用System.Collections.Generic; 利用制度全球化; 使用Newtonsoft.Json; 使用Newtonsoft.Json.Converters;
1.
3.
SF20-0178-BO
层/
24/09/2020
C级
使用系统;
使用System.Collections.Generic;
利用制度全球化;
使用Newtonsoft.Json;
使用Newtonsoft.Json.Converters;
名称空间Unitechnik_PXML_
{
公共部分类福利2
{
[JsonProperty(“PXML_文件”)]
公共PxmlDocument PxmlDocument{get;set;}
}
公共部分类文档
{
[JsonProperty(“DocInfo”)]
公共DocInfo DocInfo{get;set;}
[JsonProperty(“订单”)]
公共秩序{get;set;}
[JsonProperty(“xmlns”)]
公共Uri Xmlns{get;set;}
}
公共部分类DocInfo
{
[JsonProperty(“主要版本”)]
[JsonConverter(类型(紫色排列转换器))]
公共长主版本{get;set;}
[JsonProperty(“MinorVersion”)]
[JsonConverter(类型(紫色排列转换器))]
公共长最小值{get;set;}
[JsonProperty(“\u GlobalID”)]
公共字符串GlobalId{get;set;}
}
公共部分类序
{
[JsonProperty(“订单号”)]
公共字符串OrderNo{get;set;}
[JsonProperty(“楼层”)]
公共字符串{get;set;}
[JsonProperty(“提款日期”)]
公共字符串DrawingDate{get;set;}
[JsonProperty(“DrawingRevision”)]
[JsonConverter(类型(紫色排列转换器))]
公共长绘图修订版{get;set;}
[JsonProperty(“ApplicationName”)]
公共字符串ApplicationName{get;set;}
[JsonProperty(“ApplicationVersion”)]
公共字符串应用程序版本{get;set;}
[JsonProperty(“OrderArea”)]
公共字符串OrderArea{get;set;}
[JsonProperty(“产品”)]
公共产品产品{get;set;}
}
公共部分类积
{
[JsonProperty(“ElementNo”)]
公共字符串元素no{get;set;}
[JsonProperty(“ProductType”)]
[JsonConverter(类型(紫色排列转换器))]
公共长ProductType{get;set;}
[JsonProperty(“总厚度”)]
[JsonConverter(类型(紫色排列转换器))]
公共长总厚度{get;set;}
[JsonProperty(“DoubleWallsGap”)]
[JsonConverter(类型(紫色排列转换器))]
公共长双壁gap{get;set;}
[JsonProperty(“计件计数”)]
[JsonConverter(类型(紫色排列转换器))]
公共长计件计数{get;set;}
[JsonProperty(“TurnWidth”)]
[JsonConverter(类型(紫色排列转换器))]
公共长宽度{get;set;}
[JsonProperty(“TurnMoveX”)]
[JsonConverter(类型(紫色排列转换器))]
公共长时间移动{get;set;}
[JsonProperty(“RotationPosition”)]
[JsonConverter(类型(紫色排列转换器))]
公共长旋转位置{get;set;}
[JsonProperty(“P1X”)]
公共字符串P1X{get;set;}
[JsonProperty(“P1Y”)]
公共字符串P1Y{get;set;}
[JsonProperty(“P1Z”)]
公共字符串P1Z{get;set;}
[JsonProperty(“P2X”)]
公共字符串P2X{get;set;}
[JsonProperty(“P2Y”)]
公共字符串P2Y{get;set;}
[JsonProperty(“P2Z”)]
公共字符串P2Z{get;set;}
[JsonProperty(“P3X”)]
公共字符串P3X{get;set;}
[JsonProperty(“P3Y”)]
公共字符串P3Y{get;set;}
[JsonProperty(“P3Z”)]
公共字符串P3Z{get;set;}
[JsonProperty(“ItemPosition”)]
公共字符串ItemPosition{get;set;}
[JsonProperty(“ElementInfo”)]
公共字符串ElementInfo{get;set;}
[JsonProperty(“楼板”)]
公共楼板{get;set;}
}
公共部分类楼板
{
[JsonProperty(“SlabNo”)]
公共字符串SlabNo{get;set;}
[JsonProperty(“SlabArea”)]
公共字符串slabera{get;set;}
[JsonProperty(“SlabWeight”)]
公共字符串SlabWeight{get;set;}
[JsonProperty(“ProductionThickness”)]
[JsonConverter(类型(紫色排列转换器))]
公共长产品厚度{get;set;}
[JsonProperty(“MaxLength”)]
[JsonConverter(类型(紫色排列转换器))]
公共长最大长度{get;set;}
[JsonProperty(“MaxWidth”)]
[JsonConverter(类型(紫色排列转换器))]
公共长最大宽度{get;set;}
[JsonProperty(“项目坐标”)]
公共字符串项目坐标{get;set;}
[JsonProperty(“GenericInfo01”)]
公共字符串GenericInfo 01{get;set;}
[JsonProperty(“GenericInfo”)]
公共字符串GenericInfo02{get;set;}
[JsonProperty(“GenericInfo”)]
[JsonConverter(类型(紫色排列转换器))]
公共长泛型INFO03{get;set;}
[JsonProperty(“大纲”)]
公共大纲[]大纲{get;set;}
}
公共部分课程大纲
{
[JsonProperty(“Z”)]
公共字符串Z{get;set;}
[JsonProperty(“高度”)]
[JsonConverter(类型(紫色排列转换器))]
公共长高度{get;set;}
[JsonProperty(“名称”)]
公共字符串名称{get;set;}
[JsonProperty(“ConcreteQuality”,NullValueHandling=NullValueHandling.Ignore)]
公共字符串{get;set;}
[JsonProperty(“单位重量”)]
公共字符串单位重量{get;set;}
[JsonProperty(“卷”)]
公共字符串卷{get;set;}
[JsonProperty(“层”,NullValueHandling=NullValueHandling.Ignore)]
[JsonConverter(类型(紫色排列转换器))]
公共长?层{get;set;}
[JsonProperty(“形状”)]
公共ShapeUnion Shape{get;set;}
[JsonProperty(“\u类型”)]
公共类型枚举类型{get;set;}
[JsonProperty(“X”,NullValueHandling=NullValueHandling.Ignore)]
[JsonConverte
<?xml version="1.0" encoding="utf-8"?>
<PXML_Document xmlns="http://progress-m.com/ProgressXML/Version1">
<DocInfo GlobalID="7C7E1FC5-0A46-48c5-A3B2-249D75B70BCF">
<MajorVersion>1</MajorVersion>
<MinorVersion>3</MinorVersion>
</DocInfo>
<Order>
<OrderNo>SF20-0178-BO</OrderNo>
<Storey><Storey /></Storey>
<DrawingDate>24/09/2020</DrawingDate>
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
namespace Unitechnik_PXML_
{
public partial class Welcome2
{
[JsonProperty("PXML_Document")]
public PxmlDocument PxmlDocument { get; set; }
}
public partial class PxmlDocument
{
[JsonProperty("DocInfo")]
public DocInfo DocInfo { get; set; }
[JsonProperty("Order")]
public Order Order { get; set; }
[JsonProperty("_xmlns")]
public Uri Xmlns { get; set; }
}
public partial class DocInfo
{
[JsonProperty("MajorVersion")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long MajorVersion { get; set; }
[JsonProperty("MinorVersion")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long MinorVersion { get; set; }
[JsonProperty("_GlobalID")]
public string GlobalId { get; set; }
}
public partial class Order
{
[JsonProperty("OrderNo")]
public string OrderNo { get; set; }
[JsonProperty("Storey")]
public string Storey { get; set; }
[JsonProperty("DrawingDate")]
public string DrawingDate { get; set; }
[JsonProperty("DrawingRevision")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long DrawingRevision { get; set; }
[JsonProperty("ApplicationName")]
public string ApplicationName { get; set; }
[JsonProperty("ApplicationVersion")]
public string ApplicationVersion { get; set; }
[JsonProperty("OrderArea")]
public string OrderArea { get; set; }
[JsonProperty("Product")]
public Product Product { get; set; }
}
public partial class Product
{
[JsonProperty("ElementNo")]
public string ElementNo { get; set; }
[JsonProperty("ProductType")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long ProductType { get; set; }
[JsonProperty("TotalThickness")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long TotalThickness { get; set; }
[JsonProperty("DoubleWallsGap")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long DoubleWallsGap { get; set; }
[JsonProperty("PieceCount")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long PieceCount { get; set; }
[JsonProperty("TurnWidth")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long TurnWidth { get; set; }
[JsonProperty("TurnMoveX")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long TurnMoveX { get; set; }
[JsonProperty("RotationPosition")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long RotationPosition { get; set; }
[JsonProperty("P1X")]
public string P1X { get; set; }
[JsonProperty("P1Y")]
public string P1Y { get; set; }
[JsonProperty("P1Z")]
public string P1Z { get; set; }
[JsonProperty("P2X")]
public string P2X { get; set; }
[JsonProperty("P2Y")]
public string P2Y { get; set; }
[JsonProperty("P2Z")]
public string P2Z { get; set; }
[JsonProperty("P3X")]
public string P3X { get; set; }
[JsonProperty("P3Y")]
public string P3Y { get; set; }
[JsonProperty("P3Z")]
public string P3Z { get; set; }
[JsonProperty("ItemPosition")]
public string ItemPosition { get; set; }
[JsonProperty("ElementInfo")]
public string ElementInfo { get; set; }
[JsonProperty("Slab")]
public Slab Slab { get; set; }
}
public partial class Slab
{
[JsonProperty("SlabNo")]
public string SlabNo { get; set; }
[JsonProperty("SlabArea")]
public string SlabArea { get; set; }
[JsonProperty("SlabWeight")]
public string SlabWeight { get; set; }
[JsonProperty("ProductionThickness")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long ProductionThickness { get; set; }
[JsonProperty("MaxLength")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long MaxLength { get; set; }
[JsonProperty("MaxWidth")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long MaxWidth { get; set; }
[JsonProperty("ProjectCoordinates")]
public string ProjectCoordinates { get; set; }
[JsonProperty("GenericInfo01")]
public string GenericInfo01 { get; set; }
[JsonProperty("GenericInfo02")]
public string GenericInfo02 { get; set; }
[JsonProperty("GenericInfo03")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long GenericInfo03 { get; set; }
[JsonProperty("Outline")]
public Outline[] Outline { get; set; }
}
public partial class Outline
{
[JsonProperty("Z")]
public string Z { get; set; }
[JsonProperty("Height")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long Height { get; set; }
[JsonProperty("Name")]
public string Name { get; set; }
[JsonProperty("ConcreteQuality", NullValueHandling = NullValueHandling.Ignore)]
public string ConcreteQuality { get; set; }
[JsonProperty("UnitWeight")]
public string UnitWeight { get; set; }
[JsonProperty("Volume")]
public string Volume { get; set; }
[JsonProperty("Layer", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long? Layer { get; set; }
[JsonProperty("Shape")]
public ShapeUnion Shape { get; set; }
[JsonProperty("_Type")]
public TypeEnum Type { get; set; }
[JsonProperty("X", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long? X { get; set; }
[JsonProperty("Y", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long? Y { get; set; }
[JsonProperty("GenericInfo02", NullValueHandling = NullValueHandling.Ignore)]
public string GenericInfo02 { get; set; }
[JsonProperty("MountPartDirection", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long? MountPartDirection { get; set; }
[JsonProperty("MountPartType", NullValueHandling = NullValueHandling.Ignore)]
public string MountPartType { get; set; }
[JsonProperty("MountPartLength", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long? MountPartLength { get; set; }
[JsonProperty("MountPartWidth", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long? MountPartWidth { get; set; }
}
public partial class ShapeElement
{
[JsonProperty("SVertex")]
public SVertex[] SVertex { get; set; }
[JsonProperty("Cutout", NullValueHandling = NullValueHandling.Ignore)]
[JsonConverter(typeof(FluffyParseStringConverter))]
public bool? Cutout { get; set; }
}
public partial class SVertex
{
[JsonProperty("X")]
public string X { get; set; }
[JsonProperty("Y")]
public string Y { get; set; }
[JsonProperty("Bulge")]
[JsonConverter(typeof(PurpleParseStringConverter))]
public long Bulge { get; set; }
}
public partial class PurpleShape
{
[JsonProperty("SVertex")]
public SVertex[] SVertex { get; set; }
}
public enum TypeEnum { Lot, Mountpart };
public partial struct ShapeUnion
{
public PurpleShape PurpleShape;
public ShapeElement[] ShapeElementArray;
public static implicit operator ShapeUnion(PurpleShape PurpleShape) => new ShapeUnion { PurpleShape = PurpleShape };
public static implicit operator ShapeUnion(ShapeElement[] ShapeElementArray) => new ShapeUnion { ShapeElementArray = ShapeElementArray };
}
public partial class Welcome2
{
public static Welcome2 FromJson(string json) => JsonConvert.DeserializeObject<Welcome2>(json, Unitechnik_PXML_.Converter.Settings);
}
public static class Serialize
{
public static string ToJson(this Welcome2 self) => JsonConvert.SerializeObject(self, Unitechnik_PXML_.Converter.Settings);
}
internal static class Converter
{
public static readonly JsonSerializerSettings Settings = new JsonSerializerSettings
{
MetadataPropertyHandling = MetadataPropertyHandling.Ignore,
DateParseHandling = DateParseHandling.None,
Converters =
{
ShapeUnionConverter.Singleton,
TypeEnumConverter.Singleton,
new IsoDateTimeConverter { DateTimeStyles = DateTimeStyles.AssumeUniversal }
},
};
}
internal class PurpleParseStringConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(long) || t == typeof(long?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
long l;
if (Int64.TryParse(value, out l))
{
return l;
}
throw new Exception("Cannot unmarshal type long");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (long)untypedValue;
serializer.Serialize(writer, value.ToString());
return;
}
public static readonly PurpleParseStringConverter Singleton = new PurpleParseStringConverter();
}
internal class ShapeUnionConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(ShapeUnion) || t == typeof(ShapeUnion?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
switch (reader.TokenType)
{
case JsonToken.StartObject:
var objectValue = serializer.Deserialize<PurpleShape>(reader);
return new ShapeUnion { PurpleShape = objectValue };
case JsonToken.StartArray:
var arrayValue = serializer.Deserialize<ShapeElement[]>(reader);
return new ShapeUnion { ShapeElementArray = arrayValue };
}
throw new Exception("Cannot unmarshal type ShapeUnion");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
var value = (ShapeUnion)untypedValue;
if (value.ShapeElementArray != null)
{
serializer.Serialize(writer, value.ShapeElementArray);
return;
}
if (value.PurpleShape != null)
{
serializer.Serialize(writer, value.PurpleShape);
return;
}
throw new Exception("Cannot marshal type ShapeUnion");
}
public static readonly ShapeUnionConverter Singleton = new ShapeUnionConverter();
}
internal class FluffyParseStringConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(bool) || t == typeof(bool?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
bool b;
if (Boolean.TryParse(value, out b))
{
return b;
}
throw new Exception("Cannot unmarshal type bool");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (bool)untypedValue;
var boolString = value ? "true" : "false";
serializer.Serialize(writer, boolString);
return;
}
public static readonly FluffyParseStringConverter Singleton = new FluffyParseStringConverter();
}
internal class TypeEnumConverter : JsonConverter
{
public override bool CanConvert(Type t) => t == typeof(TypeEnum) || t == typeof(TypeEnum?);
public override object ReadJson(JsonReader reader, Type t, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null) return null;
var value = serializer.Deserialize<string>(reader);
switch (value)
{
case "lot":
return TypeEnum.Lot;
case "mountpart":
return TypeEnum.Mountpart;
}
throw new Exception("Cannot unmarshal type TypeEnum");
}
public override void WriteJson(JsonWriter writer, object untypedValue, JsonSerializer serializer)
{
if (untypedValue == null)
{
serializer.Serialize(writer, null);
return;
}
var value = (TypeEnum)untypedValue;
switch (value)
{
case TypeEnum.Lot:
serializer.Serialize(writer, "lot");
return;
case TypeEnum.Mountpart:
serializer.Serialize(writer, "mountpart");
return;
}
throw new Exception("Cannot marshal type TypeEnum");
}
public static readonly TypeEnumConverter Singleton = new TypeEnumConverter();
}
<?xml version="1.0" encoding="utf-8"?>
<PXML_Document xmlns="http://progress-m.com/ProgressXML/Version1">
<DocInfo GlobalID="7C7E1FC5-0A46-48c5-A3B2-249D75B70BCF">
<MajorVersion>1</MajorVersion>
<MinorVersion>3</MinorVersion>
</DocInfo>
<Order>
<OrderNo>SF20-0178-BO</OrderNo>
<Storey><Storey /></Storey>
<DrawingDate>24/09/2020</DrawingDate>
</Order>
</PXML_Document>
var xmlDoc = new XmlSerializer(typeof(PXML_Document)).Deserialize(File.OpenRead("OrderData.xml"));
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Data;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
XmlReader reader = XmlReader.Create(FILENAME);
XmlSerializer serializer = new XmlSerializer(typeof(PxmlDocument));
PxmlDocument pxmlDocument = (PxmlDocument)serializer.Deserialize(reader);
}
}
[XmlRoot(ElementName = "PXML_Document", Namespace = "http://progress-m.com/ProgressXML/Version1")]
public partial class PxmlDocument
{
public DocInfo DocInfo { get; set; }
public Order Order { get; set; }
}
public partial class DocInfo
{
public long MajorVersion { get; set; }
public long MinorVersion { get; set; }
}
public partial class Order
{
public string OrderNo { get; set; }
public string Storey { get; set; }
public string DrawingDate { get; set; }
public long DrawingRevision { get; set; }
public string ApplicationName { get; set; }
public string ApplicationVersion { get; set; }
public string OrderArea { get; set; }
//public Product Product { get; set; }
}
}