加载XML文件时出错{“根级别的数据无效。第1行,第1行”。}

时间:2014-01-12 18:09:32

标签: c# xml

我已经搜索了一些关于此错误的主题,但似乎没有人给我一个解决方案。

我尝试使用xml源数据首先使用代码mvc c#创建我的类的实例。

我的模型看起来像这样:

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; }
}


}

我的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>

1 个答案:

答案 0 :(得分:0)

特定错误意味着您有一个格式不正确的XML文档。您确定提供的XML是您在代码中实际引用的XML吗?虽然对我来说似乎没问题,但我建议尝试使用其他XML编辑器或Web浏览器打开它以确认它没问题。

获得此异常的另一个原因可能是路径错误且流返回null。或者您无权访问特定文件,因此从流中获取null。尝试阅读文件而不解析它只是为了检查它。

希望我帮忙!