我正在尝试反序列化在线XML文件并将结果写入数据库。但是,我收到以下错误:
“ System.InvalidOperationException:'XML文档中存在错误 (2,2)”
内部异常
“ InvalidOperationException:http://deutsche-boerse.com/dbag/app/open/xetra'>不是 预期”
下面是我的代码:
class Program
{
public static void Main()
{
Program semoAPI = new Program();
//Intraday Market Results
using (WebClient webClient = new WebClient())
{
WebClient n = new WebClient();
var json = n.DownloadString("https://reports.semopx.com/api/v1/documents/static-reports?" +
"page=1&page_size=1&order_by=ASC&ReportName=Intraday%20Market%20Results%20Order&Group=Market%20Data");
SemoReports semoReports = JsonConvert.DeserializeObject<SemoReports>(json);
Console.WriteLine("Intraday Market Results Report: ");
Console.WriteLine(semoReports.ResourceBaseUri + "/" + semoReports.Items[0].ResourceName);
string imrXML = semoReports.ResourceBaseUri + "/" + semoReports.Items[0].ResourceName;
XDocument document = XDocument.Load(imrXML);
semoAPI.DeserializeIntradayMarketResults(imrXML);
}
}
//IntradayMarketResults to Database
private void DeserializeIntradayMarketResults(string filename)
{
//Visual only not needed
Console.WriteLine("\n" + "Reading IntradayMarketResults XML File");
Console.WriteLine("===========================================================");
// Create an instance of the XmlSerializer.
XmlSerializer serializer = new XmlSerializer(typeof(IntradayMarketResults));
// Declare an object variable of the type to be deserialized.
IntradayMarketResults item;
using (XmlReader reader = XmlReader.Create(filename))
{
// Call the Deserialize method to restore the object's state.
item = (IntradayMarketResults)serializer.Deserialize(reader);
//Write out the properties of the object. (Visual Only, not needed)
Console.Write(
item.IMRReportHeader[0].exchNam + "\t" +
item.IMRReportHeader[0].envText + "\t" +
item.IMRReportHeader[0].rptCod + "\t" +
item.IMRReportHeader[0].rptName + "\t" +
item.IMRReportHeader[0].rptPrntEffDat + "\t" +
item.IMRReportHeader[0].rptPrntRunDat + "\n \n \n");
//write the properties to the db
using (SEMOContext context = new SEMOContext())
{
context.IntradayMarketResultsReports.Add(item);
context.SaveChanges();
}
}
这也是我设置课程的方式:
public class IntradayMarketResults
{
public IMRReportHeader[] IMRReportHeader { get; set; }
}
[XmlRoot(ElementName = "rptHdr")]
public class IMRReportHeader
{
[Key]
public int RrportID { get; set; }
public string exchNam { get; set; }
public string envText { get; set; }
public string rptCod { get; set; }
public string rptName { get; set; }
public string rptPrntEffDat { get; set; }
public string rptPrntRunDat { get; set; }
}
我尝试过的事情:
我尝试将以下内容添加到课程中
[Serializable, XmlRoot("tc540")]
我也尝试过:
[XmlRoot(ElementName = "tc540", DataType = "string", IsNullable = false)]
还尝试在运行时添加:
//Root error fix
XmlRootAttribute xRoot = new XmlRootAttribute();
xRoot.ElementName = "tc540";
//xRoot.Namespace = "https://deutsche-boerse.com/dbag/app/open/xetra";
xRoot.IsNullable = true;
XmlSerializer serializer = new XmlSerializer(typeof(IntradayMarketResults), xRoot);
最后我尝试了:
XmlSerializer serializer = new XmlSerializer(typeof(IntradayMarketResults), new XmlRootAttribute("tc540"));
即使尝试了所有这些解决方案,我仍然会收到相同的错误消息。
我要反序列化的XML文件链接:https://reports.semopx.com/documents/IDC_OrderFile_20190331_20190401053002.xml
PS。我知道这段代码可以正常工作,因为它可以使用相同的API处理更简单的XML文件。 我也知道console.writeline行在调试时不需要为了视觉目的。
我似乎无法使它适用于更复杂的XML文件。
链接到更简单的XML文件:https://reports.semopx.com/documents/ExchangeTransparencyData_NI-IDA3_20190401_20190401161901.xml
感谢您的任何帮助!