我正在使用ale 1.1架构。我试图从XML中提取ECReports:
var client = new HttpClient(new HttpClientHandler()
{
UseDefaultCredentials = true
});
client.BaseAddress = new Uri(Convert.ToString(ConfigurationManager.AppSettings["ServiceMethodUrl"]));
var javaScriptSerializer = new System.Web.Script.Serialization.JavaScriptSerializer();
string jsonString = javaScriptSerializer.Serialize(payload);
var httpContent = new StringContent(jsonString, Encoding.UTF8, "application/JSON");
HttpResponseMessage response=client.PostAsync("api/event/PostEventStatus", httpContent).Result;
return response.StatusCode;
我在第5行收到以下错误: 抛出异常:System.Xml.dll中的“System.InvalidOperationException”
这是InnerException消息:
没想到:ECReports reports = new ECReports();
string path = @"C:\DevTools\tag1.xml";
XmlSerializer serializer = new XmlSerializer(typeof(ECReports));
StreamReader reader = new StreamReader(path);
reports = (ECReports) serializer.Deserialize(reader);
reader.Close();
ECReports类与xml文件具有相同的命名空间:
<ECReports xmlns='urn:epcglobal:ale:xsd:1'>
这是我的XML:
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:epcglobal:ale:xsd:1")]
答案 0 :(得分:0)
我从XML文件中删除了xmlns="urn:epcglobal:ale:xsd:1"
,现在我的代码正常工作!但我很想知道另一种不删除该属性以使代码运行良好的方法
答案 1 :(得分:0)
试试这个......
... Usings
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
using System.IO;
Classes ...(使用http://xmltocsharp.azurewebsites.net/从您的XML创建)
[XmlRoot(ElementName = "member", Namespace = "urn:epcglobal:ale:xsd:1")]
public class Member
{
[XmlElement(ElementName = "tag", Namespace = "urn:epcglobal:ale:xsd:1")]
public string Tag { get; set; }
}
[XmlRoot(ElementName = "groupList", Namespace = "urn:epcglobal:ale:xsd:1")]
public class GroupList
{
[XmlElement(ElementName = "member", Namespace = "urn:epcglobal:ale:xsd:1")]
public Member Member { get; set; }
}
[XmlRoot(ElementName = "groupCount", Namespace = "urn:epcglobal:ale:xsd:1")]
public class GroupCount
{
[XmlElement(ElementName = "count", Namespace = "urn:epcglobal:ale:xsd:1")]
public string Count { get; set; }
}
[XmlRoot(ElementName = "group", Namespace = "urn:epcglobal:ale:xsd:1")]
public class Group
{
[XmlElement(ElementName = "groupList", Namespace = "urn:epcglobal:ale:xsd:1")]
public GroupList GroupList { get; set; }
[XmlElement(ElementName = "groupCount", Namespace = "urn:epcglobal:ale:xsd:1")]
public GroupCount GroupCount { get; set; }
}
[XmlRoot(ElementName = "report", Namespace = "urn:epcglobal:ale:xsd:1")]
public class Report
{
[XmlElement(ElementName = "group", Namespace = "urn:epcglobal:ale:xsd:1")]
public Group Group { get; set; }
[XmlAttribute(AttributeName = "reportName")]
public string ReportName { get; set; }
}
[XmlRoot(ElementName = "reports", Namespace = "urn:epcglobal:ale:xsd:1")]
public class Reports
{
[XmlElement(ElementName = "report", Namespace = "urn:epcglobal:ale:xsd:1")]
public List<Report> Report { get; set; }
}
[XmlRoot(ElementName = "ECReports", Namespace = "urn:epcglobal:ale:xsd:1")]
public class ECReports
{
[XmlElement(ElementName = "reports", Namespace = "urn:epcglobal:ale:xsd:1")]
public Reports Reports { get; set; }
[XmlAttribute(AttributeName = "xmlns")]
public string Xmlns { get; set; }
[XmlAttribute(AttributeName = "schemaVersion")]
public string SchemaVersion { get; set; }
[XmlAttribute(AttributeName = "creationDate")]
public string CreationDate { get; set; }
[XmlAttribute(AttributeName = "specName")]
public string SpecName { get; set; }
[XmlAttribute(AttributeName = "date")]
public string Date { get; set; }
[XmlAttribute(AttributeName = "ALEID")]
public string ALEID { get; set; }
[XmlAttribute(AttributeName = "totalMilliseconds")]
public string TotalMilliseconds { get; set; }
[XmlAttribute(AttributeName = "terminationCondition")]
public string TerminationCondition { get; set; }
}
...代码
try
{
ECReports deserializedXML = new ECReports();
// Deserialize to object
XmlSerializer serializer = new XmlSerializer(typeof(ECReports));
using (FileStream stream = File.OpenRead(@"xml.xml"))
{
deserializedXML = (ECReports)serializer.Deserialize(stream);
} // Put a break-point here, then mouse-over deserializedXML
}
catch (Exception)
{
throw;
}
此代码将按原样反序列化您的XML并且没有错误....将您的XML保存到与.EXE相同的文件夹中的文件(xml.xml)....希望有所帮助。