[英]Error Loading XML File {“Data at the root level is invalid. Line 1, position 1.”}
I have searched a few topics on this error but none of them seem to be giving me a resolution. 我搜索了一些有关此错误的主题,但似乎没有一个给我解决方法。
I have trying to use xml source data to create an instance of my class using code first mvc c#. 我试图使用xml源数据使用代码优先mvc c#创建我的类的实例。
My Model looks like this: 我的模型如下所示:
namespace PSA.Models
{
public class PunchRoundRule:IPunchRoundRule
{
public virtual int PunchRoundRuleId { get; set; }
public virtual string Name { get; set; }
[Display(Name="In Punch Early Inside Round")]
public virtual string InPunchEarlyInsideRound { get; set; }
[Display(Name = "Unscheduled Out Round")]
public virtual string UnscheduledOutRound { get; set; }
[Display(Name = "In Punch Early Outside Grace")]
public virtual string InPunchEarlyOutsideGrace { get; set; }
[Display(Name = "In Punch Early Outside Round")]
public virtual string InPunchEarlyOutsideRound { get; set; }
[Display(Name = "Out Punch Early Change Point")]
public virtual string OutPunchEarlyChangePoint { get; set; }
[Display(Name = "Out Punch Early Inside Grace")]
public virtual string OutPunchEarlyInsideGrace { get; set; }
[Display(Name = "Out Punch Late Outside Round")]
public virtual string OutPunchLateOutsideRound { get; set; }
[Display(Name = "Out Punch Late Inside Round")]
public virtual string OutPunchLateInsideRound { get; set; }
[Display(Name = "In Punch Late Change Point")]
public virtual string InPunchLateChangePoint { get; set; }
[Display(Name = "Transfer Round")]
public virtual string TransferRound { get; set; }
[Display(Name = "Unscheduled Out Grace")]
public virtual string UnscheduledOutGrace { get; set; }
[Display(Name = "Transfer Grace")]
public virtual string TransferGrace { get; set; }
[Display(Name = "Is Missed Out Exception")]
public virtual bool IsMissedOutException { get; set; }
[Display(Name = "Out Punch Late Inside Grace")]
public virtual string OutPunchLateInsideGrace { get; set; }
[Display(Name = "In Punch Late Outside Round")]
public virtual string InPunchLateOutsideRound { get; set; }
[Display(Name = "In Punch Late Outside Grace")]
public virtual string InPunchLateOutsideGrace { get; set; }
[Display(Name = "In Punch Early Change Point")]
public virtual string InPunchEarlyChangePoint { get; set; }
[Display(Name = "Unscheduled In Round")]
public virtual string UnscheduledInRound { get; set; }
[Display(Name = "In Punch Early Inside Grace")]
public virtual string InPunchEarlyInsideGrace { get; set; }
[Display(Name = "Out Punch Late Outside Grace")]
public virtual string OutPunchLateOutsideGrace { get; set; }
[Display(Name = "In Punch Late Inside Round")]
public virtual string InPunchLateInsideRound { get; set; }
[Display(Name = "In Punch Late Inside Grace")]
public virtual string InPunchLateInsideGrace { get; set; }
[Display(Name = "Use Scheduled Out")]
public virtual bool UseScheduledOut { get; set; }
[Display(Name = "Out Punch Early Outside Round")]
public virtual string OutPunchEarlyOutsideRound { get; set; }
[Display(Name = "Out Punch Early Outside Grace")]
public virtual string OutPunchEarlyOutsideGrace { get; set; }
[Display(Name = "Out Punch Early Inside Round")]
public virtual string OutPunchEarlyInsideRound { get; set; }
[Display(Name = "Unscheduled In Grace")]
public virtual string UnscheduledInGrace { get; set; }
[Display(Name = "Out Punch Late Change Point")]
public virtual string OutPunchLateChangePoint { get; set; }
public virtual int WorkRuleId { get; set; }
public virtual WorkRule WorkRule { get; set; }
[ScaffoldColumn(false)]
public virtual DateTime DateSubmitted { get; set; }
IEnumerable<PunchRoundRule> punchRoundRule = GetPunchRoundRule<PunchRoundRule>("http://localhost:12276/Files/WSAPunchRoundRule.xml");
static IEnumerable<T> GetPunchRoundRule<T>(String xml)
where T : IPunchRoundRule, new()
{
return XElement.Parse(xml)
.Elements("Response")
.Select(f => new T
{
PunchRoundRuleId=10,
Name = f.Element("name").Value,
});
}
public PunchRoundRule()
{
this.DateSubmitted = DateTime.Now;
}
}
public interface IPunchRoundRule
{
int PunchRoundRuleId { get; set; }
string Name { get; set; }
}
}
My Xml File looks like this: 我的Xml文件如下所示:
<?xml version='1.0' encoding="utf-8"?>
<Response Status="Success" action="RetrieveAllForUpdate">
<WSAPunchRoundRule InPunchEarlyInsideRound="2:00" UnscheduledOutRound="0:01" InPunchEarlyOutsideGrace="0:00" InPunchEarlyOutsideRound="0:01" OutPunchEarlyChangePoint="0:00" OutPunchEarlyInsideGrace="0:00" OutPunchLateOutsideRound="0:01" OutPunchLateInsideRound="0:01" InPunchLateChangePoint="0:00" TransferRound="0:01" UnscheduledOutGrace="0:00" TransferGrace="0:00" IsMissedOutException="true" OutPunchLateInsideGrace="0:00" InPunchLateOutsideRound="0:01" InPunchLateOutsideGrace="0:00" InPunchEarlyChangePoint="2:00" UnscheduledInRound="0:01" InPunchEarlyInsideGrace="0:00" OutPunchLateOutsideGrace="0:00" InPunchLateInsideRound="0:01" InPunchLateInsideGrace="0:00" UseScheduledOut="false" OutPunchEarlyOutsideRound="0:01" Name="Default Early Start" OutPunchEarlyOutsideGrace="0:00" OutPunchEarlyInsideRound="0:01" UnscheduledInGrace="0:00" OutPunchLateChangePoint="0:00"/>
<WSAPunchRoundRule InPunchEarlyInsideRound="2:00" UnscheduledOutRound="0:01" InPunchEarlyOutsideGrace="0:00" InPunchEarlyOutsideRound="0:01" OutPunchEarlyChangePoint="0:00" OutPunchEarlyInsideGrace="0:00" OutPunchLateOutsideRound="0:01" OutPunchLateInsideRound="0:01" InPunchLateChangePoint="0:00" TransferRound="0:01" UnscheduledOutGrace="0:00" TransferGrace="0:00" IsMissedOutException="true" OutPunchLateInsideGrace="0:00" InPunchLateOutsideRound="0:01" InPunchLateOutsideGrace="0:00" InPunchEarlyChangePoint="2:00" UnscheduledInRound="0:01" InPunchEarlyInsideGrace="0:00" OutPunchLateOutsideGrace="0:00" InPunchLateInsideRound="0:01" InPunchLateInsideGrace="0:00" UseScheduledOut="false" OutPunchEarlyOutsideRound="0:01" Name="Ramadan" OutPunchEarlyOutsideGrace="0:00" OutPunchEarlyInsideRound="0:01" UnscheduledInGrace="0:00" OutPunchLateChangePoint="0:00"/>
<WSAPunchRoundRule InPunchEarlyInsideRound="2:00" UnscheduledOutRound="0:01" InPunchEarlyOutsideGrace="0:00" InPunchEarlyOutsideRound="0:01" OutPunchEarlyChangePoint="0:00" OutPunchEarlyInsideGrace="0:00" OutPunchLateOutsideRound="0:01" OutPunchLateInsideRound="0:01" InPunchLateChangePoint="0:00" TransferRound="0:01" UnscheduledOutGrace="0:00" TransferGrace="0:00" IsMissedOutException="false" OutPunchLateInsideGrace="0:00" InPunchLateOutsideRound="0:01" InPunchLateOutsideGrace="0:00" InPunchEarlyChangePoint="2:00" UnscheduledInRound="0:01" InPunchEarlyInsideGrace="0:00" OutPunchLateOutsideGrace="0:00" InPunchLateInsideRound="0:01" InPunchLateInsideGrace="0:00" UseScheduledOut="true" OutPunchEarlyOutsideRound="0:01" Name="Transfer Rounding" OutPunchEarlyOutsideGrace="0:00" OutPunchEarlyInsideRound="0:01" UnscheduledInGrace="0:00" OutPunchLateChangePoint="0:00"/>
<WSAPunchRoundRule InPunchEarlyInsideRound="2:00" UnscheduledOutRound="0:01" InPunchEarlyOutsideGrace="0:00" InPunchEarlyOutsideRound="0:01" OutPunchEarlyChangePoint="0:00" OutPunchEarlyInsideGrace="0:00" OutPunchLateOutsideRound="0:01" OutPunchLateInsideRound="0:01" InPunchLateChangePoint="0:00" TransferRound="0:01" UnscheduledOutGrace="0:00" TransferGrace="0:00" IsMissedOutException="true" OutPunchLateInsideGrace="0:00" InPunchLateOutsideRound="0:01" InPunchLateOutsideGrace="0:00" InPunchEarlyChangePoint="2:00" UnscheduledInRound="0:01" InPunchEarlyInsideGrace="0:00" OutPunchLateOutsideGrace="0:00" InPunchLateInsideRound="0:01" InPunchLateInsideGrace="0:00" UseScheduledOut="false" OutPunchEarlyOutsideRound="0:01" Name="Workrule Rounding" OutPunchEarlyOutsideGrace="0:00" OutPunchEarlyInsideRound="0:01" UnscheduledInGrace="0:00" OutPunchLateChangePoint="0:00"/>
</Response>
The specific error implies that you have a not-well formed XML document. 特定的错误意味着您有一个格式不正确的XML文档。 Are you sure that the provided XML is the one you are actually referencing in your code?
您确定所提供的XML是您在代码中实际引用的XML吗? While it seems OK to me, I would suggest trying to open it with another XML editor or a web browser to confirm that it is OK.
虽然对我来说似乎可以,但是我建议尝试使用另一个XML编辑器或Web浏览器将其打开,以确认它可以。
Another reason for getting this exception could be if you have a wrong path and the stream returns null
. 导致此异常的另一个原因可能是路径错误并且流返回
null
。 Or you could have no rights to access the specific file and therefore get null
out of the stream. 或者,您可能无权访问特定文件,因此从流中获取
null
。 Try to read the file without parsing it just to check it out. 尝试读取文件而不解析它只是将其签出。
Hope I helped! 希望我能帮上忙!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.