简体   繁体   中英

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#.

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 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. Are you sure that the provided XML is the one you are actually referencing in your code? 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.

Another reason for getting this exception could be if you have a wrong path and the stream returns null . Or you could have no rights to access the specific file and therefore get null out of the stream. Try to read the file without parsing it just to check it out.

Hope I helped!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM