gpt4 book ai didi

c# - XMLSerializer : was not expected

转载 作者:太空宇宙 更新时间:2023-11-03 15:22:06 26 4
gpt4 key购买 nike

我正在尝试反序列化如下所示的 XML:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<ns2:ECReports xmlns:ns2="urn:epcglobal:ale:xsd:1" xmlns:ns3="urn:epcglobal:ale:wsdl:1" specName="Cycle_MDEAirport_1" date="2016-04-25T15:06:19.980Z"
ALEID="RIFIDI-ALE1158647263" totalMilliseconds="9492" terminationCondition="DURATION">
<reports>
<report reportName="Cycle_MDEAirport">
<group>
<groupList>
<member>
<epc>303400c0e4a3f48000a2f8d5</epc>
</member>
</groupList>
</group>
</report>
</reports>
<ECSpec includeSpecInReports="true">
<logicalReaders>
<logicalReader>MDEAirport</logicalReader>
</logicalReaders>
<boundarySpec>
<repeatPeriod unit="MS">10000</repeatPeriod>
<duration unit="MS">9500</duration>
<stableSetInterval unit="MS">0</stableSetInterval>
</boundarySpec>
<reportSpecs>
<reportSpec reportName="Cycle_MDEAirport" reportIfEmpty="true" reportOnlyOnChange="false">
<reportSet set="ADDITIONS" />
<output includeEPC="true" includeTag="true" includeRawHex="true" includeRawDecimal="true" />
</reportSpec>
</reportSpecs>
</ECSpec>
</ns2:ECReports>

但我得到以下错误:

<ImplementationException xmlns='urn:epcglobal:ale:wsdl:1'> was not expected

这是我的代码:

XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "ECReports";
xRoot.Namespace = "urn:epcglobal:ale:xsd:1";
xRoot.IsNullable = true;

XmlSerializer serializer = new XmlSerializer(typeof(ECReports), xRoot);
MemoryStream ms = new MemoryStream(e.Message);
ECReports ECReports;
ECReports = (ECReports)serializer.Deserialize(ms);

我正在使用的类 ECReports 来自服务引用(它不是我开发的类)

最佳答案

我发现了问题。您要从 namespace 转到非 namespace ,因此您必须将 namespace 指定为“”。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

using System.Xml.Linq;

namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:\temp\test.xml";
static void Main(string[] args)
{
byte[] message = File.ReadAllBytes(FILENAME);
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "ECReports";
xRoot.Namespace = "urn:epcglobal:ale:xsd:1";
xRoot.IsNullable = true;

XmlSerializer serializer = new XmlSerializer(typeof(ECReports), xRoot);
MemoryStream ms = new MemoryStream(message);
ECReports ECReports;
ECReports = (ECReports)serializer.Deserialize(ms);
}
}
[XmlRoot("ECReports")]
public class ECReports
{
[XmlAttribute("specName")]
public string specName { get; set; }

[XmlAttribute("date")]
public DateTime date { get; set; }

[XmlAttribute("ALEID")]
public string aleid { get; set; }

[XmlAttribute("totalMilliseconds")]
public int totalMilliseconds { get; set; }

[XmlAttribute("terminationCondition")]
public string terminationCondition { get; set; }

[XmlElement(ElementName = "reports", Namespace = "")]
public Reports reports { get; set; }

[XmlElement(ElementName = "ECSpec", Namespace = "")]
public ECSpec ecSpec { get; set; }
}


[XmlRoot(ElementName = "reports", Namespace = "")]
public class Reports
{
[XmlElement("report")]
public Report report { get; set; }
}
[XmlRoot("report")]
public class Report
{
[XmlAttribute("reportName")]
public string reportName { get; set; }

[XmlElement("group")]
public Group group { get; set; }

}
[XmlRoot("group")]
public class Group
{
[XmlElement("groupList")]
public GroupList groupList { get; set; }

}
[XmlRoot("groupList")]
public class GroupList
{
[XmlElement("member")]
public Member member { get; set; }

}
[XmlRoot("member")]
public class Member
{
[XmlElement("epc")]
public string epc { get; set; }
}

[XmlRoot("ECSpec", Namespace = "")]
public class ECSpec
{
[XmlAttribute("includeSpecInReports")]
public Boolean includeSpecInReports { get; set; }

[XmlElement("logicalReaders")]
public LogicalReaders logicalReaders { get; set; }

[XmlElement("boundarySpec")]
public BoundarySpec boundarySpec { get; set; }

[XmlElement("reportSpecs")]
public ReportSpecs reportSpecs { get; set; }

}
[XmlRoot("logicalReaders")]
public class LogicalReaders
{
[XmlElement("logicalReader")]
public string logicalReader { get; set; }
}

[XmlRoot("boundarySpec")]
public class BoundarySpec
{
[XmlElement("repeatPeriod")]
public Unit repeatPeriod { get; set; }

[XmlElement("duration")]
public Unit duration { get; set; }

[XmlElement("stableSetInterval")]
public Unit stableSetInterval { get; set; }
}

[XmlRoot("reportSpecs")]
public class ReportSpecs
{
[XmlElement("reportSpec")]
public ReportSpec reportSpec { get; set; }
}
[XmlRoot("")]
public class Unit
{
[XmlAttribute("unit")]
public string unit { get; set; }

[XmlText]
public int value { get; set; }

}
[XmlRoot("reportSpec")]
public class ReportSpec
{
[XmlAttribute("reportName")]
public string reportName { get; set; }

[XmlAttribute("reportIfEmpty")]
public Boolean reportIfEmpty { get; set; }

[XmlAttribute("reportOnlyOnChange")]
public Boolean reportOnlyOnChange { get; set; }

[XmlElement("reportSet")]
public ReportSet reportSet { get; set; }

[XmlElement("output")]
public Output output { get; set; }
}

[XmlRoot("reportSet")]
public class ReportSet
{
[XmlAttribute("set")]
public string set { get; set; }
}

[XmlRoot("output")]
public class Output
{
[XmlAttribute("includeEPC")]
public Boolean includeEPC { get; set; }

[XmlAttribute("includeTag")]
public Boolean includeTag { get; set; }

[XmlAttribute("includeRawHex")]
public Boolean includeRawHex { get; set; }

[XmlAttribute("includeRawDecimal")]
public Boolean includeRawDecimal { get; set; }
}

}

关于c# - XMLSerializer : <ImplementationException xmlns ='urn:epcglobal:ale:wsdl:1' > was not expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36845256/

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